Posts

Donate

Sorting Of <li> Elements Using jQuery sort() Method Not Working (Refresh Page Only) In ASP.NET

Given the simple sorting code below which changes the order of <li> elements to ascending order and assuming that the text content of each li elements are numbers. $( function () { $( '#sortList' ).click( function (e) { // Select all the li items var numbersList = $( '#numbersList li' ); numbersList.sort( function (a, b) { return parseInt ($(a).text(), 10 ) > parseInt ($(a).text(), 10 ); }).appendTo(numbersList.parent()); }); }); The code should work only to find out that the page will only refresh after the button click. And then the re-arranging of the elements are discarded. The simple fix to this issue is to add the preventDefault() to prevent the button's default behavior that is to post back. $( function () { $( '#sortList' ).click( function (e) { e.preventDefault(); var numbersList = $( '#numbersList li' ); numbersList.sort( function (a, b) { var temp1 = parseInt($

$.getJSON() Not Loading JSON File Within Visual Studio Project In ASP.NET Web Forms

Hi, I was trying to load a JSON file located within my Visual Studio project using $.getJSON(), however the code below doesn't work as expected. $.getJSON( '/Assets/misc/employee.json' , function (data) { alert( 'processing!' ); }) .done( function (r) { alert(r.message); }) .fail( function (s) { alert( 'oops the file is missing!' ); }); After investigating for a few hours, I tested the local path of the JSON file such as http://localhost:3766/Assets/misc/employee.json and the result was an exception " HTTP Error 404.3 - Not Found The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map. ". I remember the solution in one of my ASP.NET project was to set the MimeMap in the web.config inside the system.webServer element. After setting the MimeMap for json files, I can now load local JSON files using $.getJSON(). <system.web

Visual Studio Showing Only "Attach" And No Debug Options In ASP.NET Project

Hello, Last night my computer shuts down unexpectedly due to power outage and when I opened an ASP.NET project this morning, it only shows "Attach" in the debug options. Running to different browsers such as Google Chrome, Firefox and Internet Explorer were missing. After a few search, I encountered a solution which is to set again the ASP.NET Project as StartUp Project by right clicking the project and selecting the option Set as StartUp Project . That's it.. :-)

Set An Html.RadioButtonFor() Helper As Default Checked In ASP.NET MVC

Good day! Given that you have to set a radio button as default checked in razor view, a code to do that is presented below using conditional ternary operator. However, there is an issue with the code shown below. Even if you set the checked property with empty string, it will still render as checked during run-time. @foreach ( var answer in item.Answers) { <p> @Html.RadioButtonFor(m => answer.ID, true , new { @disabled = "true" , @Name = item.ID, @checked = (answer.IsCorrect) ? "checked" : "" })@answer.AnswerText </p> } To ignore that issue, there are several ways to fix that. First one is to use if statement in the view itself . @foreach ( var answer in item.Answers) { <p> @if (answer.IsCorrectAnswer) { @Html.RadioButtonFor(m => answer.ID, true , new { @disabled = "true" , @Name = item.ID, @checked = "true" })@a

How To Handle ASP.NET GridView TemplateFields Null Values In Aspx Markup

Hello, To handle null values in GridView TemplateFields through aspx/html markup rather than code-behind, you can simply use the language operators to those fields. One approach is to use the conditional ternary operator in C# and VB.NET. The sample codes below illustrates how to do in C# and VB.NET. For C#, use the (?) operator. While VB.NET uses If() operator. C#.NET Code <asp:TemplateField HeaderText= "Name" SortExpression= "EmpName" > <ItemTemplate> <asp:Label ID= "lbl" runat= "server" Text= '<%# Eval("EmpName") %>' ></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID= "txtEmployee" runat= "server" Text= '<%# Eval("EmpName") == System.DBNull.Value ? string.Empty : Eval("EmpName").ToString() %>' ></asp:TextBox> </EditItemTemplate> </asp:TemplateField> VB.NET Code <as

View In ASP.NET MVC Not Refreshing After Calling Ajax Post In jQuery UI

Hello, In an application wherein the controller is invoked using Ajax POST and that controller will redirect to a landing page such as Index after processing, an issue will occur such as the View of that landing page isn't updated or not refreshed. Normally, the View will be updated since the model is queried from the database. [HttpPost] public ActionResult Delete( int id) { try { Customer customer = _customer.Customers.FirstOrDefault(t => t.CustomerID == id); if (customer != null ) { _customer.Customers.Remove(customer); _customer.SaveChanges(); } } catch { //TODO } return RedirectToAction( "Index" ); } The fix for that is to change the RedirectToAction() statement to return the url of a landing page as JSON result. And in the ajax statement, add a statement that navigate to the url returned from the controller. [HttpPost] public ActionResult Delete( int id) { try { Customer customer = _customer.Customers.FirstOrDefault(t =>

Url Variable Returns Forward Slash "/" instead of absolute Url in Controller Action

Hi, When passing a url from a controller action to an Ajax $.post() result that is to navigate to the landing page, using the code below generates only a forward slash which is the root symbol instead of an absolute url. If the url value (/) will be used in window.location.href , it will cause an IIS page not found error. var url = new UrlHelper(Request.RequestContext).Action( "Index" , "Customer" ); return Json( new { Url = url }); So, to return the absolute url of the controller action in the current context, replace UrlHelper class with Url.Action() method. It is important to include the Request.Url.Scheme in the parameter to pass the correct protocol of the url. The code below will return an absolute url such as: http://localhost:7088 for the landing page. var url = this .Url.Action( "Index" , "Customer" , null , Request.Url.Scheme); return Json( new { Url = url });

window.location.href not opening website

Hello, The code below does not open the link, instead opens a blank page or IIS error. This issue occurs in all major browsers. window.location.href = 'www.nba.com' ; In order to fix the issue,you must prepend the correct protocol before the website such as (http:// or https:// or ftp://). If the protocol is omitted, the link is interpreted as a file in the website. window.location.href = 'http://www.nba.com' ; Cheers!

ASP.NET MVC CRUD(Create/Update/Delete) With Dapper ORM in VB.NET

Image
Konnichiwa, Here's a VB.NET ASP.NET MVC CRUD project using Dapper ORM which is the counterpart of this post ASP.NET MVC with Dapper ORM . The only file in the Models folder that has updates is Customer.cs class such as applying Display and DisplayAttributes. Imports System.ComponentModel.DataAnnotations Public Class Customer Public Property CustomerID() As Integer Get Return m_CustomerID End Get Set (value As Integer ) m_CustomerID = value End Set End Property Private m_CustomerID As Integer <Display(Name:= "Company Name" )> Public Property CompanyName() As String Get Return m_CompanyName End Get Set (value As String ) m_CompanyName = value End Set End Property Private m_CompanyName As String <Display(Name:= "Address" )> Public Property Address() As String

ASP.NET MVC CRUD(Create/Update/Delete) With Dapper ORM

Image
Hi All, Here's an ASP.NET MVC CRUD(Create/Update/Delete) project with Dapper ORM. The model classes and interface are based from this post Using Dapper ORM in ASP.NET Web Forms with few modifications in Customer class. The updates are adding Display attribute and DisplayFormat attribute to CreditLimit and IntroDate properties. using System; using System.ComponentModel.DataAnnotations; namespace ASPMVCDapper.Models { public class Customer { public int CustomerID { get ; set ; } [Display(Name="Company Name")] public string CompanyName { get ; set ; } [Display(Name = "Address")] public string Address { get ; set ; } [Display(Name = "City")] public string City { get ; set ; } [Display(Name = "State")] public string State { get ; set ; } [Display(Name = "Intro Date")] [DisplayFormat(DataFormatString = "{0:yyyy-MM

Donate