I have a controller that looks something like this:
if (ModelState.IsValid)
{
...
}
else
{
ViewBag.Msg = "error";
return View();
}
I’ve added the ViewBag.Msg to log the behavior of the controller. If the form isn’t valid it should show the proper error messages acording to the model validators, but it’s not working because the “error” string is being displayed on the view.
How can i track the error. Acording to the documentation ModelState has a property Errors but visual studio is saying that this is not true.
Is there another way log the error and what could it be.
Edit:
I already have the @Html.ValidatorSumary(true), thats the wierd thing about this case, i know that ModelState is not valid because of the ViewBag.Msg line, but it’s not showing a thing.
Edit2
It happens that I had a wrong value for one of the helpers like this
@Html.RadioButtonFor(x => x.BoolField, 1)
and the model
public Nullable<Bool> boolField { get; set; }
You have to return the Model back to the view to show the Model erros in the View. If you want to log the Model Errors, you can check the
ViewData.ModelState.Valuescollection to see the errors.