包装/多模型视图的客户端验证

发布于 2024-12-02 19:44:05 字数 1649 浏览 1 评论 0原文

我将这两个模型类包装在一个模型中。强类型视图和服务器端验证工作得很好,但我无法让客户端验证适用于此包装的模型类。

这是我的客户端代码:

Sys.Mvc.ValidatorRegistry.validators.usernameEmail = function (rule) {

    var emailProperty = rule.ValidationParameters.emailProperty;
    var message = rule.ErrorMessage;

    return function (value, context) {
        if (!value || !value.length) {
            return true;
        }

        var usernameField = context.fieldContext.elements[0];
        var emailFieldId = $('input[name = "' + emailProperty + '"]').attr('id');
        var emailField = $get(emailFieldId, usernameField.form);

        //...validation stuff...

    };
};

问题是 emailProperty 仅返回其模型中指定的属性名称,即。 “EmailAddress”,但强类型视图将名称指定为“Model1Name.EmailAddress”,因为它的调用方式如下:

<%: Html.TextBoxFor(m => m.Model1Name.EmailAddress)%>

因此,在查找属性时它返回 null,并且出现错误。所以我想我的问题是:

  1. 有没有办法获得分配的模型名称(“Model1Name”),所以我 可以将其附加到属性名称吗?
  2. 我可以从 DataAnnotations/ModelValidationRule 类传递分配的模型名称吗?如果是这样,怎么办?

以下是我的模型类:

public class Model1
{
    public string EmailAddress{ get; set; }
    ...
}

public class Model2
{
    [UsernameEmail]
    public string Username{ get; set; }
    ...
}

public class WrappedModel
{
    public Model1 Model1Name{ get; set; }
    public Model2 Model2Name { get; set; }
}

旁注:客户端验证对于单个模型视图效果很好。

编辑 经过多次调试,我发现

viewContext.ViewData.TemplateInfo.GetFullHtmlFieldId("EmailAddress");

在我的 DataAnnotationsModelValidator 中仅返回参数名称“EmailAddress”,而忽略了其前缀“Model1Name”,从而返回错误的 ID。

任何帮助将不胜感激。谢谢。

I have these 2 model classes wrapped in one model. The strongly typed view and server side validation works well enough but I can't get the client-side validation working for this wrapped model class.

Here's my client-side code:

Sys.Mvc.ValidatorRegistry.validators.usernameEmail = function (rule) {

    var emailProperty = rule.ValidationParameters.emailProperty;
    var message = rule.ErrorMessage;

    return function (value, context) {
        if (!value || !value.length) {
            return true;
        }

        var usernameField = context.fieldContext.elements[0];
        var emailFieldId = $('input[name = "' + emailProperty + '"]').attr('id');
        var emailField = $get(emailFieldId, usernameField.form);

        //...validation stuff...

    };
};

The problem is that emailProperty returns only the property name stated in its model ie. "EmailAddress" but the strongly typed view assigns the name as "Model1Name.EmailAddress" since it is called like:

<%: Html.TextBoxFor(m => m.Model1Name.EmailAddress)%>

Thus it returns null when looking for the property and I get an error. So I guess my questions here would be:

  1. Is there a way to get the assigned model name ("Model1Name") so I
    could append it with the property name?
  2. Could I pass the assigned model name from the DataAnnotations/ModelValidationRule classes? If so, how?

Here are my model classes:

public class Model1
{
    public string EmailAddress{ get; set; }
    ...
}

public class Model2
{
    [UsernameEmail]
    public string Username{ get; set; }
    ...
}

public class WrappedModel
{
    public Model1 Model1Name{ get; set; }
    public Model2 Model2Name { get; set; }
}

Side-note: The client side validation works well for single model views.

Edit
After much debugging, I found out that

viewContext.ViewData.TemplateInfo.GetFullHtmlFieldId("EmailAddress");

in my DataAnnotationsModelValidator is returning only the parameter name "EmailAddress" and is leaving out its prefix "Model1Name" thus returning the wrong Id.

Any help would be appreciated. Thanks.

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

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

发布评论

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

评论(1

汐鸠 2024-12-09 19:44:05

您的自定义 [UsenameEmail] 属性负责发出正确的客户端验证元数据。更准确地说,应该为此注册验证器适配器。您需要确保适配器生成完整属性名称,即“Model1Name.EmailAddress”,然后“usernameEmail”客户端函数将接收正确的字段名称并按预期运行。

有关更多详细信息,请参阅“DataAnnotationsModelValidatorProvider.RegisterAdapter(...)”。
这是实现示例 - http://weblogs.asp.net/jacqueseloff/archive/2009/12/17/asp-net-mvc-2-rc-validating-credit-card-numbers.aspx

Your custom [UsenameEmail] attribute is a piece which is responsible for emitting correct client-side validation metadata. To be more precise, there should be validator adapter registered for that. You need to make sure adapter generates full property name, i.e. "Model1Name.EmailAddress", then "usernameEmail" client-side function will receive correct field name and will operate as expected.

See "DataAnnotationsModelValidatorProvider.RegisterAdapter(...)" for more details.
Here is implementation sample - http://weblogs.asp.net/jacqueseloff/archive/2009/12/17/asp-net-mvc-2-rc-validating-credit-card-numbers.aspx

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