开发者

MVC2, using the same EF Entity in various Views

开发者 https://www.devze.com 2023-01-23 17:48 出处:网络
i have this problem that has been buggin me for the last hours. Lets suppose i have this Signup form, that i need to fill it up, all propertieshave the RequiredAttribute, the model is a EF entity na

i have this problem that has been buggin me for the last hours.

  1. Lets suppose i have this Signup form, that i need to fill it up, all propertieshave the RequiredAttribute, the model is a EF entity named "User".

  2. i have this second edit account details form, and at this moment a specific field ("Username") is no longer required, because i already have it, the user doesnt need to fil it again and in matter of fact it doest enven show up on the Edit form.

The problem: when posting the second Edit form, obviously i am stucked with the Username RequiredAttribute.

I would solve this just by adding a "Bind" attribute with "Exclude" option, BUT, this is my current model :

public class AccountDetailsModel
{
    public User user { get; set; }
    public string NEWPASSWORD1 { get; set; } // ne开发者_Go百科w password
    public string NEWPASSWORD2 { get; set; } // new password comparison
}

and just looks like Bind Attribute with Exclude option doesnt handle complex Model types. I cant get it to work on this scenario.

Im stuck, scratching my heads for a long time now...

How can i overcome this?

I just want to re-use my EF Entity (User) on 2 different views along with its DataAnnotations.

Thanks in advance.


I just want to re-use my EF Entity (User) on 2 different views along with its DataAnnotations.

Here's the problem. You shouldn't do this. I would recommend you setting up view models which are classes specifically tailored for a given view and contain the necessary validation attributes for this view only. To ease the mapping between your EF models and the view models you could use AutoMapper.


Put the UserName in a hidden input field for the details page.

0

精彩评论

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