Sometimes it happens that an error occurs somewhere inside the MVC assembly, which is not handled beautifully and which is not copied to your model state, as expected. Then, when you try to display in your view Html.ValidationSummary, it does not show you an error, which can be very confusing. One example that may cause the model binding process to fail, which I wrote about here here . As a rule, after you find out why this happens, you can make corrections to your code and no longer worry about it.
, , , :
public static IDictionary<string, string> GetModelStateErrors(this ViewDataDictionary viewDataDictionary)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (var modelStateKey in viewDataDictionary.ModelState.Keys)
{
var modelStateValue = viewDataDictionary.ModelState[modelStateKey];
foreach (var error in modelStateValue.Errors)
{
var errorMessage = error.ErrorMessage;
var exception = error.Exception;
if (!String.IsNullOrEmpty(errorMessage))
{
dict.Add(modelStateKey, "Egads! A Model Error Message! " + errorMessage);
}
if (exception != null)
{
dict.Add(modelStateKey, "Egads! A Model Error Exception! " + exception.ToString());
}
}
}
return dict;
}
, Model, :
var x = ViewData.GetModelStateErrors();
UpdateModel. x , .
!