I have a ASP.NET MVC application and want to have validation on both level - view model validation and domain model validation based on data annotation attributes. View Model validation works simple:
public class CustomerFormVM
{
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
[Required]
[Display(Name = "Street")]
public string Street { get; set; }
[Required]
[Display(Name = "City")]
public string City { get; set; }
[Required]
[Display(Name = "State")]
public string State { get; set; }
}
and then call in controller:
if (ModelState.IsValid)
{
I have the same domain model class:
public class CustomerFormPoco
{
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
[Required]
[Display(Name = "Street")]
public string Street { get; set; }
[Required]
[Display(Name = "City")]
public string City { get; set; }
[Required]
[Display(Name = "State")]
public string State { get; set; }
}
but how to validate it?
// viewmodel is CustomerFormVM object
var pocoModel = mapper.Map<CustomerFormPoco>(viewmodel);
if I don't check 'viewmodel' variable then I get 'pocoModel' variable with nullable Name, Street, City...
How to call validation and make decision depend on result?
Requiredattributes that should be simple. Then if it passes then you don't have to worry about validation on the server-side. - GrizzlyRequiredannotations for strings - stackoverflow.com/questions/23939738/… - ethane