ASP.NET MVC 3:模型级客户端或远程验证

发布于 2024-12-14 19:13:42 字数 565 浏览 1 评论 0 原文

我需要在 MVC 3 编辑页面中进行一些模型级验证。 (具体来说,我需要确认字段 A 或字段 B 已填写,但不能同时填写,也不能两者都填写。)我

想要执行客户端验证以及服务器端验证,这意味着要么使用远程验证或实施重复的验证代码。我都同意。

我读过许多关于滚动自己的服务器端模型级验证的文章,但没有一篇文章涉及实现客户端验证。 (我不知道——我确信有人可以告诉我——模型级客户端验证是否很容易使用 jQuery 验证进行设置。)

我还阅读了有关实现您自己的远程验证的信息从头开始,我可能必须这样做,因为 Remote 属性只是属性级别的。

我已阅读这个问题,与我的相同,但唯一真正正确的链接似乎并没有说明回答者所说的内容。

所以,我的问题是:是否有一种简单、相对省力的方法来实现服务器+客户端模型级验证,无论是否有远程组件?有没有一篇不错的博客文章或网页可以解释这一点?

I need to do some model-level validation in an MVC 3 edit page. (To be specific, I need to confirm that either Field A or Field B is filled in, but not both and not neither.)

I want to perform client-side validation as well as server-side validation, which means either using remote validation or implementing duplicate validation code. I'm OK with either.

I've read a number of posts on rolling your own server-side model-level validation, but none that deal with also implementing client side validation. (I don't know -- I'm sure someone out there can tell me -- whether model-level client-side validation is easy to set up with jQuery validation.)

I've also read about implementing your own remote validation from scratch, which I may have to do since the Remote attribute is property-level only.

I've read this question, which is identical to mine, but the only link that's really on-point doesn't seem to say what the answerer says it says.

So, my question: is there an easy, relatively low-effort way to implement server+client model-level validation, with or without a remote component? And is there a nice blog post or web page somewhere that explains this?

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

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

发布评论

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

评论(3

乱了心跳 2024-12-21 19:13:42

我认为 Scott Kirkland 的数据注释扩展 正是您想要的。
这里是一篇博客文章他写了关于他的扩展的文章。

核心库提供可在任何 .NET 4.0 项目中使用的服务器端验证属性(无 MVC 依赖项)。还有一个易于插入的客户端验证库,可在 ASP.NET MVC 3 项目中使用不显眼的 jquery 验证(仅需要包含 MVC3 的 javascript 文件)。

I think Data Annotation Extention by Scott Kirkland does exactly what you want.
Here is a blog post he wrote about his extensions.

The core library provides server-side validation attributes that can be used in any .NET 4.0 project (no MVC dependency). There is also an easily pluggable client-side validation library which can be used in ASP.NET MVC 3 projects using unobtrusive jquery validation (only MVC3 included javascript files are required).

卖梦商人 2024-12-21 19:13:42

此处回答了类似的问题,这可能会有所帮助?给出的答案是为了验证至少输入了一个字段,但答案中给出的原则可能正是您正在寻找的,并且您应该能够更改答案以适应您所需的验证。该解决方案还提供服务器端和客户端验证选项,我相信您可以将该解决方案用作模块或属性级别验证?

此外,还有以下文章 此处详细介绍了如何创建您自己的自定义验证,类似于我链接到的答案中提供的验证。

希望这有帮助。

There is a similar question answered here which may be of some help? The answer given is for validating that at least one field is entered but the principles given in the answer may be what you are looking for and you should be able to change the answer to suit the validation you require. The solution also offers both server and client side validation options and I believe you can use the solution as both module or property level validation?

Additionally, there is also the following article here detailing how to create your own custom validation similar to that provided in the answer I linked to.

Hope this helps.

只有影子陪我不离不弃 2024-12-21 19:13:42

如果我没理解错的话,mvc 3 实际上包含 jquery 客户端验证。
首先,对于模型级服务器端验证,您可以使用自己的验证规则覆盖默认的 isValid 函数,如下所示(涉及多字段):

 public sealed class PropertyAAttribute : ValidationAttribute
{
    public string propertyBAttribute { get; set; }
    public override bool IsValid(object value)
    {
        // Your validation rule here
    }
}

[PropertyA(propertyBAttribute = "PropertyB")]
public object PropertyA {get;set;}
public object PropertyB {get;set;}

然后,要处理客户端,您可以简单地使用包含的 jquery 验证功能:

            var frm = $('#formData');

            frm.validate();

像这样,您将根据您在模型中定义的规则在客户端收到错误消息。
希望这是您所需要的:)

If i get you right, mvc 3 do actually include jquery client-side validation.
First, for the model-level server side validation, you can override the default isValid function with your own validation rules, something like this (involve multi-field):

 public sealed class PropertyAAttribute : ValidationAttribute
{
    public string propertyBAttribute { get; set; }
    public override bool IsValid(object value)
    {
        // Your validation rule here
    }
}

[PropertyA(propertyBAttribute = "PropertyB")]
public object PropertyA {get;set;}
public object PropertyB {get;set;}

Then, to deal with client side, you can simply use the included jquery validation function:

            var frm = $('#formData');

            frm.validate();

Like this, you will have the error message at client side based on the rule you defined in the model.
Hope this is what you need :)

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