I am having trouble displaying validation summary, I only want to display errors in validation summary, not beside the field. In my view i have,
@Html.ValidationSummary()
@Html.ValidationMessageFor(m =>m.Name)
in my controller i have
ModelState.AddModelError("Name",
"name is required");
Am i not supposed to get a validation error message? at the top? I don't get what i am missing...
I am also including these script files.. jquery.validate.min.js jquery.validate.unobtrusive.min.js
Try
@Html.ValidationSummary(false)
so that it will not exclude property errors.
OR
Try the method @xurca linked which is to add the model error with an empty key so it is not tied to a specific property.
ModelState.AddModelError(String.Empty, "name is required");
If you custom named your field like
@Html.TextBoxFor(model => model.Name, new { id = "staffSearchFilterName", value = "", **Name = "staffSearchFilterName"** })
then you have to use
@Html.ValidationMessage("staffSearchFilterName")
If your @Html.ValidationSummary() call is in a Partial view, DON'T pass data into the partial view like this:
@Html.Partial("_YourForm", Model, new ViewDataDictionary { { "Submit", true }})
Instead, add the key value pair to Html.ViewData collection first:
@{
Html.ViewData.Add(new KeyValuePair<string,object>("Submit",true));
}
then call the partial view:
@Html.Partial("_YourForm", Model, Html.ViewData)
this will allow the ModelState to propagate to the partial view properly.
In controller custom error
ModelState.AddModelError("test","test");
Summary:
@Html.ValidationSummary(false, "", new { @class = "text-danger" })
Individual field:
@Html.TextBoxFor(m => m.FieldName, new { @class= "form-control" })
@Html.ValidationMessageFor(m => m.FieldName, "", new { @class = "text-danger" })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.