using System.Security.Claims; var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); Note that this extension method only works for Users inside controllers, not view components, as the view component User is of IPrincipal.
Tag: asp.net
How to use if else block to decide style in Razor
Declare a local variable at the beginning of the View: @{ var yourStyle = “”; if(Model.Condition) { yourStyle = “float:left;”; //specific conditions and logic } else { yourStyle = “float:right;”; //any other conditions and logic } } and then just use it in your div: <div style=”@yourStyle”>
How to get output return value from SqlDataSource in WebForms
When returning a string from a stored procedure as a return parameter, we must set it to a non-null value before the stored proc is called – even if it’s an output-only parameter. So we’ll need to make your parameter have a direction of InputOutput and set a default value: <asp:Parameter Name=”MyReturnParameter” Type=”String” Direction=”InputOutput” DefaultValue=”A […]
How to use client-side function on checkbox or any asp.net component
Here is the Asp part: <label for=”chkCondition”> <asp:CheckBox ID=”chkCondition” Text=”Should i show the div?” runat=”server” onclick=”ShowHideDiv(this)” /> </label> <hr /> <div id=”divToShowOrHide” style=”display: none”> this div will be shown regarding the checkbox checked status. <asp:TextBox ID=”myTextBox” runat=”server” /> </div> And here is the Javascript part: <script type=”text/javascript”> function ShowHideDiv(chkCondition) { var divToShowOrHide = document.getElementById(“divToShowOrHide”); divToShowOrHide.style.display […]
How to remove searching, filtering, ordering and info from Asp.NET MVC Datatable
var table = $(“#datatable”).DataTable({ “paging”: false, “ordering”: false, “searching”: false, “info”: false });
How to make a checkbox or dropdown readonly in Razor
You can add @disabled = “disabled” to the last parameter named HtmlAttributes. @Html.CheckBox(“someNameForYourCheckBox”, CheckedValue (True or Not), new { @disabled = “disabled” }) @Html.DropDownList(“someNameForYourDropDown”, YourSelectList (comes from Model), new { @disabled = “disabled” })
How to add scroll bar to div in Asp.Net
Let’s say we have div in view <div id=”myDiv” class=”div-with-scrollbar”> </div> With this CSS file, you can add a scrollbar .div-with-scrollbar { overflow-y: scroll; width: 100%; height: 500px; }
How to get the integrity value for a jquery version of script reference in a web page
You can use https://www.srihash.org/ to generate links. https://code.jquery.com/jquery-3.6.0.min.js will be generated as <script src=”https://code.jquery.com/jquery-3.6.0.min.js” integrity=”sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==” crossorigin=”anonymous”></script> You can get all the versions of jquery from here: https://releases.jquery.com/jquery/ Thanks to mscdeveloper for such great post.
How to get session value in Javascript
<script> let mySession = ‘<%= Session[“SessionName”].ToString() %>’; //use your session … console.log(mySession); alert(mySession); </script>
How to use ? : if statements with Asp.Net Razor
<span class=”btn btn-@(item.Value == 1 ? “primary” : item.Value == 2 ? “warning” : “danger”)”>EVALUATE</span> Briefly, we can say that @(condition ? “the value, if the condition is true” : “the value, if the condition is false” )
Getting index value in Razor ForEach
@{int i = 0;} @foreach(var myItem in Model.Members) { <span>@i</span> @{i++;} }
Populating Javascript array with Model in ASP.Net MVC Razor
We just need to loop through the razor collection to populate the Javascript array as below: @foreach (var item in Model) { @:$(function () { @:$(“#map”).googleMap(); @:$(“#map”).addMarker({ @:coords:[@item.Latitude], @:title: `@item.City, @item.District`, @:text: `@item.Address` @:}); @:}); }
Getting Session value in Javascript & Asp.Net
<script> var mySessionValue = ‘<%= Session[“SessionName”].ToString() %>’; alert(mySessionValue); </script>
How to render HTML tags Asp.Net MVC
string myValue = “<b>Hello</b>”; @Html.Raw(HttpUtility.HtmlDecode(myValue))
How to refresh a DIV content with Javascript
To reload a section of the page, you could use jquerys load with the current url and specify the fragment you need, which would be the same element that load is called on, in our below case #myDivId function updateDivContent() { $(“#myDivId”).load(window.location.href + ” #myDivId” ); }
Creating Multiline textbox using Html.Helper function in Asp.Net MVC
@Html.TextArea(“Body”, null, new { cols = “55”, rows = “10” }) or
How to split string and get first (or n-th) value only
string myValueToSplit = “Istanbul, Tokyo, Seoul, Bangkok, Bali, Minsk, Belgrade, Kiev”; var theFirstValue = myValueToSplit.Split(‘,’)[0]; //OUTPUT: theFirstValue = “Istanbul” //You can change [0] as you wish. //For example, if you want to get Kiev, make it [7]
Formatting a Nullable DateTime with ToString() in ASP.Net MVC
Let’s say we have a Nullable DateTime named StartTime [DisplayFormat(DataFormatString = “{0:dd.MM.yyyy}”)] public Nullable<System.DateTime> StartTime { get; set; }