Posts

Donate

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

Unable to cast object of type 'DapperRow' to return Type

Aloha, Given the following code which generates an exception "Unable to cast object of type 'DapperRow", the cause for this is that the return type of FirstOrDefault() dynamic. public Customer FindById( int Id) { return this ._db.Query( "SELECT * FROM Customer WHERE CustomerID=@Id" , new { Id = Id }).FirstOrDefault(); } In order to solve this error, you have several options. One is to use Query.<TReturn>() instead of Query() wherein you can explicity specify the type. public Customer FindById(int Id) { return this._db.Query<Customer>( "SELECT * FROM Customer WHERE CustomerID=@Id" , new { Id = Id }).FirstOrDefault(); } Another option is to modify the code with issue, that is to store the result of the query in a dynamic variable and then assign the dynamic properties value to the class properties. public Customer FindById( int Id) { dynamic customerRecord = this ._db.Query( "SELECT * FROM Customer WHERE CustomerID=@Id&qu

Using Dapper ORM in ASP.NET Web Forms (Visual Basic.NET)

Image
Hi, This is a conversion of this post Using Dapper ORM in ASP.NET WebForm to VB.NET language. Customer.vb 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 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 Public Property Address() As String Get Return m_Address End Get Set (value As String ) m_Address = Value End Set End Property Private m_Address As String Public Property City() As String Get Return m_City End Get Set (

Using Dapper ORM In ASP.NET Web Forms

Image
Hello, This is a simple tutorial of using Dapper Micro ORM in an ASP.NET Webform application. According to Wikipedia, Dapper is an object-relational mapping (ORM) product for the Microsoft .NET platform: it provides a framework for mapping an object-oriented domain model to a traditional relational database. Its purpose is to relieve the developer from a significant portion of relational data persistence-related programming tasks. The key feature of Dapper is performance as presented in Dapper website (github). It is second to Hand coded SQLDataReader class when querying specific number of records. To get started with, create an Empty ASP.NET WebForm project and then add the Dapper to our project via NuGet. Make sure to alter the connection string in Web.Config to point to your database. Then we need to add one interface and three classes in our Models folder which applies the idea of Repository. Customer.cs public class Customer { public int CustomerID { get ; set ; } publ

Donate