asp.net-mvc Mvc 4 验证摘要错误未显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18258611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:05:27  来源:igfitidea点击:

Mvc 4 validation summary errors not displaying

asp.net-mvcc#-4.0asp.net-mvc-4

提问by WingMan20-10

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

我还包括这些脚本文件.. jquery.validate.min.js jquery.validate.unobtrusive.min.js

回答by asymptoticFault

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.

尝试使用@xurca 链接的方法,该方法使用空键添加模型错误,使其不绑定到特定属性。

ModelState.AddModelError(String.Empty, "name is required");

回答by Vicentiu Berneanu

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")

回答by Wei

If your @Html.ValidationSummary() call is in a Partial view, DON'T pass data into the partial view like this:

如果您的 @Html.ValidationSummary() 调用在 Partial 视图中,请不要像这样将数据传递到局部视图中:

@Html.Partial("_YourForm", Model,  new ViewDataDictionary { { "Submit", true }})

Instead, add the key value pair to Html.ViewData collection first:

相反,首先将键值对添加到 Html.ViewData 集合中:

@{
    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.

这将允许 ModelState 正确传播到局部视图。

回答by ArulKumar

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" })