Posts

Showing posts with the label Data Annotations

Donate

ASP.NET Core MVC Model Validation Using Data Annotation Attributes And Bootstrap 4

Image
Hi All! In this post, I'll demonstrate how to perform a model validation to a form in ASP.NET Core 3.1 MVC using Data Annotation Attributes and Bootstrap 4+ with reference to jQuery unobtrusive validation scripts to show error on the page. This concept has been applied since the early days of ASP.NET until it's recent release of .NET 5. I have applied this solution to some of the projects I've worked either ASP.NET MVC 4 or ASP.NET MVC 5. Enough talk and let's get down to business by applying this topic in a ASP.NET Core 3.1 project. First is to create an ASP.NET Core 3.1 MVC project using Visual Studio 2019 and add Bootstrap 4.0 package via NuGet as this will be used to style our form. In our Models folder, add an Employee class that contains common properties to describe an employee such as ID, Name and related data. Most of the properties are decorated with DataAnnotations attribute except for the Dependents since I'm only using a boolean value for that. pu

Restrict Remote Validation In ASP.NET MVC Edit Mode.

Hello, In data entry operations, we normally validate user input if they exist in the database, if yes we throw some kind of exception or error message that the data they entered already exists in the database. But in scenario like editing of existing information, we don't want this to happen. So to restrict the remote validation in ASP.NET MVC, I found the fix from stack Remote validation restrict for edit controller method but modified the logic in the controller which is to return a JSON rather than a bool value. The code modifications are as follows. Edit View or Edit Partial View: Add another HiddenField for Initial Product Code used for Comparison. @Html.Hidden("InitProductCode", Model.ProductCode) Model: Add AdditionalFields in Remote Attribute. [Display(Name = "Product Code")] [Required(ErrorMessage = "ProductCode is required")] [Remote("CheckProductCode", "Products", HttpMethod = "POST", ErrorMessage = &q

Data Annotation Validation Of TimeSpan Property In Model Using RegularExpression Attribute

Hello, To validate a model property of type TimeSpan using Data Annotation RegularExpression attribute, the expressions should check the hours, minutes and seconds since the corresponding database column in SQL Server that is mapped with this field has a Time data type. The example below validates a 23 hour format military time and does not allow minutes or seconds greater than zero. [RegularExpression(@"^(?:[01][0-9]|2[0-3]):[0-0][0-0]:[0-0][0-0]$ ", ErrorMessage =" Invalid time format and hh:mm:ss values. ")]

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

ASP.NET MVC String.Format() Not Showing Correct Format In Views Using jQuery Calendar

Image
Greetings, Formatting display output of dates in asp.net mvc should work using String.Format() with custom date formatting criteria. However, applying String.Format() in asp.net ascx view does not display the desired format that I want. Example markup: @Html.TextBoxFor(model => model.HireDate, String.Format( "{0:g}" , Model.HireDate)) Given Date Input: 2/6/2013 12:00:00 AM Desired Output should be with time portion removed: 2/6/2013 Solution: The trick to display the desired format was to decorate a display format attribute in my model class HireDate property: private DateTime hDay; [DisplayName("Hire Date")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] public DateTime HireDate { get { if (hDay == DateTime.MinValue) { return DateTime.Today; } else return hDay; } set { if ( value == DateTime.MinValue) { hDay = DateTime.

Donate