将错误消息添加到@Html.ValidationSummary

发布于 2024-12-11 19:55:22 字数 311 浏览 1 评论 0原文

我使用标准 MVC3 Razor 视图和不显眼的 Javascript 验证,并使用 @Html.ValidationSummary 将它们显示在表单顶部。如果标准验证(例如 [Required])通过,我就会运行一些非常自定义的客户端验证,当用户点击“提交”按钮时会触发该验证。 (验证会检查多个表单元素,以确保检查了正确的一组表单元素等,因此它并不像为单个字段创建新的自定义验证器那么简单)。

我希望我在那里构建的可能错误显示在 ValidationSummary 列表中,但我不知道如何让错误消息出现在那里。

I'm using standard MVC3 Razor views with unobtrusive Javascript validation, using @Html.ValidationSummary to show them at the top of the form. If the standard validations (things like [Required]) pass, I then run some very custom client-side validation that fires when the user hits the Submit button. (The validation looks across a number of form elements to make sure that the proper set of them have been checked, etc., so it's not as simple as just creating a new custom validator for a single field).

I'd like the possible error(s) I construct there to be shown in the ValidationSummary list, but I can't figure out how to get the error message to appear there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无悔心 2024-12-18 19:55:22

在客户端:

function YourCustomValidator() {
    // do your validation logic here via JavaScript
    return true; // or false based on your validation logic
}
$(document).ready(function () {
    // take your own form-selector like ("form", this)
    $("form", this).first().submit(function () {
        return (YourCustomValidator() && $(this).valid());
    });
});

或在服务器端:

认为您有一个这样的模型:

public class Test {
    [Required]
    [StringLength(100)]
    public string FullName { get; set; }
}

当您验证它时:

if(ModelState.IsValid) { // default validations run here
    if(/* some custom validations run here, there is an error about "FullName" */){
        // you should set the "key" for Model-Error to "FullName"
        ModelState.AddModelError("FullName","error-message goes here")
    }
    if(/* some custom validations run here, the error is global, not on "FullName" */){
        // you should set the "key" for Model-Error to an empty-string
        ModelState.AddModelError("","error-message goes here")
    }
    // also you can test for model-errors again like this:
    if(ModelState.IsValid) { // if you add any error above, this will be "false"

    }
}

In client-side:

function YourCustomValidator() {
    // do your validation logic here via JavaScript
    return true; // or false based on your validation logic
}
$(document).ready(function () {
    // take your own form-selector like ("form", this)
    $("form", this).first().submit(function () {
        return (YourCustomValidator() && $(this).valid());
    });
});

OR In server-side:

Think you have a model like this:

public class Test {
    [Required]
    [StringLength(100)]
    public string FullName { get; set; }
}

and when you are validating it:

if(ModelState.IsValid) { // default validations run here
    if(/* some custom validations run here, there is an error about "FullName" */){
        // you should set the "key" for Model-Error to "FullName"
        ModelState.AddModelError("FullName","error-message goes here")
    }
    if(/* some custom validations run here, the error is global, not on "FullName" */){
        // you should set the "key" for Model-Error to an empty-string
        ModelState.AddModelError("","error-message goes here")
    }
    // also you can test for model-errors again like this:
    if(ModelState.IsValid) { // if you add any error above, this will be "false"

    }
}
私藏温柔 2024-12-18 19:55:22

您只需将 Error Message 添加到 ModelState 即可,只要您调用了 ValidationSummary() 即可显示错误消息你的观点。

要将错误添加到 ModelState 只需执行以下操作:

ModelState.AddModelError("ColumnNameOrErrorMessageKeyInState","error message goes here")

You can do so just adding the Error Message to the ModelState should display the error message for you, provided that you have ValidationSummary() called on your view.

To add the error to the ModelState just do this:

ModelState.AddModelError("ColumnNameOrErrorMessageKeyInState","error message goes here")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文