自定义会员资格提供程序上的 ModelState

发布于 2024-10-14 08:46:49 字数 871 浏览 7 评论 0原文

我已经实现了一个自定义会员资格提供程序并具有以下课程;

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 技术交流群。

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

发布评论

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

评论(1

情徒 2024-10-21 08:46:49

使用自定义配置文件类作为操作输入而不是视图模型似乎很奇怪:

public class ProfileViewModel
{
    [Required]
    public string Title { get; set; }
}

然后在控制器中,您可以使用 AutoMapper 在视图模型和模型类之间进行转换,这将更新配置文件:

[HttpPost]
[Authorize]
public ActionResult EditInvestorRegistration(ProfileViewModel profileViewModel)
{
    ProfileCommon profileCommon = AutoMapper.Map<ProfileViewModel, ProfileCommon>(profileViewModel);
    ...
}

It seems strange that you are using a custom profile class as action input instead of a view model:

public class ProfileViewModel
{
    [Required]
    public string Title { get; set; }
}

and then in your controller you could use AutoMapper to convert between the view model and the model class which will update the profile:

[HttpPost]
[Authorize]
public ActionResult EditInvestorRegistration(ProfileViewModel profileViewModel)
{
    ProfileCommon profileCommon = AutoMapper.Map<ProfileViewModel, ProfileCommon>(profileViewModel);
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文