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
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
2) On the form submit, you are calling the ModelState.IsValid property and hence validating the model passed to the action method
[HttpPost]
public ActionResult CarList(CarList model)
{
if (ModelState.IsValid)
{
//Save or whatever you want to do
}
return View(model);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With