You may need to display all errors on page when your model state is not valid.
In Asp.net MVC3 we can use ModelState property to control current state of our model.
When ModelState.IsValid == false,
I am collecting all errors in a ViewBag to display them.
In your controller :
if(ModelState.IsValid)
{
}
else
{
string errorString = "";
foreach (var modelStateValue in ViewData.ModelState.Values)
{
foreach (var error in modelStateValue.Errors)
{
var errorMessage = error.ErrorMessage;
var exception = error.Exception;
errorString += errorMessage + "\n";
}
}
ViewBag.Errors = errorString;
return View();
}
In your View :
<div class="validations">
@Html.Raw(Html.Encode(@ViewBag.Errors).Replace("\n", "<br />"))
</div>
If you want to display them in RED use CSS below
<style type="text/css">
div.validations
{
color: Red;
}
</style>
good luck
No comments:
Post a Comment