开发者

ModelState on Custom membership provider

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]开发者_如何学JAVA
    [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?


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);
    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜