使用 MvcContrib FluentHtml 进行 ASP.NET MVC 客户端验证

发布于 2024-09-30 18:25:41 字数 659 浏览 2 评论 0原文

使用内置 MVC2 代码和 MvcContrib 的 FluentHtml 构建器进行客户端验证的推荐方法是什么?我们正在使用 jQuery 客户端验证代码,而不是默认的 Microsoft AJAX 内容,如果这很重要的话(尽管我认为不应该)。

看来只有当您在页面上放置验证消息 (Html.ValidationMessageFor(x => x.FirstName)) 时,客户端验证才会注册到 jQuery Validate。 MvcContrib 的 FluentHtml this.ValidationMessage(x => x.FirstName) 仅适用于服务器端的 ModelState,如果没有错误,则不会写出任何 HTML,并且不会在客户端使用 jQuery Validate 注册给定的属性。

所以我的问题是:有没有一种方法可以让 MvContrib 的当前主干构建与 MVC2 的内置客户端验证一起工作,现在有点轻松?如果是这样,怎么办?如果没有,是否还有其他推荐的客户端验证(除了 xVal,我们目前正在使用它,但它已被弃用)?是否应该在 MvcContrib 中对此进行修补以使其正常工作?最后的手段是转向使用 ASP.NET MVC 的内置输入构建器,但我们已经在 MvcContrib 上投入了大量资金,但我们不愿意这样做。

谢谢!

What's the recommended way to do client-side validation using the built-in MVC2 code with MvcContrib's FluentHtml builders? We're using the jQuery client-side validation code, not the default Microsoft AJAX stuff, if that matters (though I don't think it should).

It seems the client-side validation only gets registered with jQuery Validate when you place a validation message (Html.ValidationMessageFor(x => x.FirstName)) on the page. MvcContrib's FluentHtml this.ValidationMessage(x => x.FirstName) only works with ModelState on the server side, doesn't write out any HTML if there's no error, and doesn't register the given property with jQuery Validate on the client-side.

So my question: is there a way to make the current trunk build of MvContrib work with MVC2's built-in client-side validation somewhat painlessly right now? If so, how? If not, is there another client-side validation that's recommended (other than xVal, which we're currently using and has been depreciated)? Should this be patched in MvcContrib so it works properly? A last resort would be to move to using ASP.NET MVC's built-in input builders, but we already invested a lot in MvcContrib's and would rather not.

Thanks!

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

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

发布评论

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

评论(2

烂柯人 2024-10-07 18:25:41

我处于完全相同的情况......我看到这篇文章,并在下面有有趣的评论,尽管我无法完全让它发挥作用。

http://lunaverse.wordpress。 com/2008/11/24/mvc Fluenthtml- Fluent-html-interface-for-ms-mvc/

如果您能理解的话,最好将其发布到此处。

保罗

Im in the exact same situation...i came across this post with in interesting comment further down although I couldn't quite get it to work.

http://lunaverse.wordpress.com/2008/11/24/mvcfluenthtml-fluent-html-interface-for-ms-mvc/

If you can make any sense of it would be good to post it back up here.

Paul

屋顶上的小猫咪 2024-10-07 18:25:41

我从 Paul 的博客文章中得到了评论,并将其修改为使用所有已知的 MVC 验证适配器,而不仅仅是必需的适配器(基本上模仿了框架本身中的大部分内容)。它如何显示错误消息并使用我们已有的内容变得有点麻烦,我为 MVC Contrib 实现了一个补丁来使用它,但最终我暂时放弃,直到 MVC3 完成并且 MVC Contrib 是针对它构建的。当更新版本即将发布时,经历这一切是没有意义的。

这就是我最终得到的结果(FluentViewPage 是我们添加行为的地方):

public class ClientsideValidationBehavior<T> : IBehavior<IMemberElement> where T : class
{
    private readonly FluentViewPage<T> _viewPage;

    public ClientsideValidationBehavior(FluentViewPage<T> viewPage)
    {
        _viewPage = viewPage;
    }

    public void Execute(IMemberElement element)
    {
        var attribute = element.GetAttribute<ValidationAttribute>();

        if (attribute == null)
        {
            return;
        }

        var formContext = _viewPage.ViewContext.FormContext;
        var fieldMetadata = formContext.GetValidationMetadataForField(UiNameHelper.BuildNameFrom(element.ForMember), true);

        var modelMetadata = ModelMetadata.FromStringExpression(element.ForMember.Member.Name, _viewPage.ViewData);
        var validators = ModelValidatorProviders.Providers.GetValidators(modelMetadata, _viewPage.ViewContext);

        validators.SelectMany(v => v.GetClientValidationRules()).ForEach(fieldMetadata.ValidationRules.Add);

        fieldMetadata.ValidationMessageId = element.ForMember.Member.Name + "_Label";
    }
}

希望对一些人有所帮助。

I got the comment from that blog article working Paul, and modified it to use all the known MVC validation adapters instead of just the Required one (basically mimicking much of what's in the framework itself). It gets kind of hairy with how it displays the error message and working with what we already have, and I implemented a patch for MVC Contrib to work with it, but in the end I'm giving up for now until MVC3 is finialized and MVC Contrib builds against it. No point in going through all this when there's an updated release coming soon.

Here's what I ended up with (FluentViewPage<T> is where we add behaviors):

public class ClientsideValidationBehavior<T> : IBehavior<IMemberElement> where T : class
{
    private readonly FluentViewPage<T> _viewPage;

    public ClientsideValidationBehavior(FluentViewPage<T> viewPage)
    {
        _viewPage = viewPage;
    }

    public void Execute(IMemberElement element)
    {
        var attribute = element.GetAttribute<ValidationAttribute>();

        if (attribute == null)
        {
            return;
        }

        var formContext = _viewPage.ViewContext.FormContext;
        var fieldMetadata = formContext.GetValidationMetadataForField(UiNameHelper.BuildNameFrom(element.ForMember), true);

        var modelMetadata = ModelMetadata.FromStringExpression(element.ForMember.Member.Name, _viewPage.ViewData);
        var validators = ModelValidatorProviders.Providers.GetValidators(modelMetadata, _viewPage.ViewContext);

        validators.SelectMany(v => v.GetClientValidationRules()).ForEach(fieldMetadata.ValidationRules.Add);

        fieldMetadata.ValidationMessageId = element.ForMember.Member.Name + "_Label";
    }
}

Hope that helps some.

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