I have this class:
public class Product
{
[Required(ErrorMessage = "empty name")]
public string Name { get; set; }
[Required(ErrorMessage = "empty description")]
public string Description { get; set; }
}
and I have a ResponseProduct which is shown in view:
public class ProductInsertVM
{
public string Message = "Success";
public Product Product;
}
In my view I have this:
@using (Html.BeginForm()){
@Html.ValidationSummary(false)
@Html.TextBoxFor(m => m.Product.Description)
@Html.ValidationMessageFor(m => m.Product.Description)
}
What I want to know is why ValidationMessageFor doest’n work? !! only ValidationSummary works. If I respond with a Product, so it works.
The code you provided should work fine if the following conditions are met.
1) You have reference to the jQuery validation scripts in your view
2) On the form submit, you are calling the ModelState.IsValid property and hence validating the model passed to the action method