自定义会员资格提供程序上的 ModelState
我已经实现了一个自定义会员资格提供程序并具有以下课程;
public class ProfileCommon : ProfileBase
{
#region Members
[Required(ErrorMessage="Required")]
public virtual string Title
{
get { return ((string)(this.GetPropertyValue("Title"))); }
set { this.SetPropertyValue("Title", value); }
}
然后,我在我的控制器中想要执行以下操作;
[HttpPost]
[Authorize]
public ActionResult EditInvestorRegistration(FormCollection collection)
{
ProfileCommon profileCommon= new ProfileCommon();
TryUpdateModel(profileCommon);
当错误中不包含标题时,这种方法会失败;
对象“Models.ProfileCommon”上的属性访问器“Title”抛出以下异常:“未找到设置属性“Title”。”
如果我去掉属性 [Required...
它工作正常,但现在我不再对我的对象进行自动验证。
现在,我知道我可以一次检查每个属性并解决问题,但我非常想使用 DataAnnotations 来为我完成这项工作。
有什么想法吗?
I have implemented a custom membership provider and have the following class;
public class ProfileCommon : ProfileBase
{
#region Members
[Required(ErrorMessage="Required")]
public virtual string Title
{
get { return ((string)(this.GetPropertyValue("Title"))); }
set { this.SetPropertyValue("Title", value); }
}
I then, in my controller want to do the following;
[HttpPost]
[Authorize]
public ActionResult EditInvestorRegistration(FormCollection collection)
{
ProfileCommon profileCommon= new ProfileCommon();
TryUpdateModel(profileCommon);
This kinda fails when title is not included with the error;
Property accessor 'Title' on object 'Models.ProfileCommon' threw the following exception:'The settings property 'Title' was not found.'
If I get rid of the attribute [Required...
it works fine but now I no longer have automatic validation on my object.
Now, I know I could check each property at a time and get around the issue but I'd dearly like to use DataAnnotations to do the work for me.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用自定义配置文件类作为操作输入而不是视图模型似乎很奇怪:
然后在控制器中,您可以使用 AutoMapper 在视图模型和模型类之间进行转换,这将更新配置文件:
It seems strange that you are using a custom profile class as action input instead of a view model:
and then in your controller you could use AutoMapper to convert between the view model and the model class which will update the profile: