Skip to main content

Posts

Showing posts from 2017

ASP.NET MVC: Custom Validation by Data Annotation

Your model should implement an interface IValidatableObject . Put your validation code in Validate method. public class MyModel : IValidatableObject {     public string Title { get; set; }     public string Description { get; set; }     public IEnumerable Validate(ValidationContext validationContext)     {         if (Title == null)             yield return new ValidationResult("*", new [] { nameof(Title) });         if (Description == null)             yield return new ValidationResult("*", new [] { nameof(Description) });     } } This is a server-side validation. It doesn't work on client-side. You validation will be performed only after form submission.

JQUERY Data table model become Null while posting Data to controller in MVC 5

Problem : JQUERY Data table model become Null while posting Data to controller in MVC 5. Solutions:  Use FOR LOOP instead of FOREACH loop to bind model for each table row.  @for(var i = 0;i < Model.People.Count;i++) {             @Html.TextBoxFor(m => Model.People[i].Name)         @Html.TextBoxFor(m => Model.People[i].Age)     }