StringLength属性,客户端验证和成员资格提供 [英] StringLength attribute, client side validation and a membership provider

查看:118
本文介绍了StringLength属性,客户端验证和成员资格提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何通过从成员资格提供程序(从web.config中获取)的值,以验证在默认MVC 3项目?AccountModels属性

How do I pass a value from Membership Provider (taken from web.config) to Validation Attributes in AccountModels in default MVC 3 project?

Membership.MinRequiredPasswordLength

从web.config中和Register.cshtml观点获得

返回值使用它:

returns value obtained from web.config and Register.cshtml view uses it:

<p>
Passwords are required to be a minimum of @Membership.MinRequiredPasswordLength
characters in length.
</p>

但似乎在视图模型文件AccountModels有值硬codeD:

But it seems that ViewModel in AccountModels file have the values hard-coded in:

[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }

那么,如何传递的值从web.config中MinimumLength参数?

So how do I pass the value from web.config to MinimumLength parameter?

推荐答案

您将无法指定属性属性动态像你想。这就是为什么模板有它硬codeD。仍然使用数据注解的解决办法是让你的视图模型实现IValidatableObject并将其对证Membership.MinRequiredPasswordLength密码。另一种选择是创建一个从ValidationAttribute和反对Membership.MinRequiredPasswordLength检查继承的属性。

You won't be able to specify an attribute property dynamically like you would like. That is why the templates have it hard-coded. The workaround to still use data annotations would be to have your view model implement IValidatableObject and have it check the password against Membership.MinRequiredPasswordLength. Another option would be to create an attribute that inherits from ValidationAttribute and checks against Membership.MinRequiredPasswordLength.

大卫·海登有一个<一个href=\"https://web.archive.org/web/20120130082206/http://davidhayden.com/blog/dave/archive/2010/12/31/ASPNETMVC3ValidationIValidatableObject.aspx\"相对=nofollow>后覆盖这两个选项。

有关客户端,你需要实现对模型或自定义属性IClientValidatable。这里是另一个<一个href=\"http://stackoverflow.com/questions/4747184/perform-client-side-validation-for-custom-attribute/4747466#4747466\">answer表示一个例子。您还需要添加客户端验证功能,你可以使用@ Membership.MinRequiredPasswordLength您的Razor视图里面的值来拉。

For the client side, you would need to implement IClientValidatable on the model or the custom attribute. Here is another answer that shows an example. You would also need to add the client side validation function, and you could use @Membership.MinRequiredPasswordLength inside your Razor view to pull in the value.

这篇关于StringLength属性,客户端验证和成员资格提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆