dor
dor

Reputation: 105

MVC 3 how to disable data annotations in entity property

public class Clubber
{
    public virtual int ObjectID { get; set;}
    public virtual User OwnerUser { get; set; }
    public virtual int BlackPoint { get; set; }
    public virtual bool ToSendSMS { get; set; }
}

and

public class User
{
    public virtual int ObjectID {get; set;}

    [Required]
    public virtual Permission Permission { get; set; }
}

and i try to make Dropdown list for the OwnerUser property by the ObjectID

@Html.DropDownList("OwnerUser.ObjectID", (SelectList)ViewBag.OwnerList)

and when I try to save its says that Permission Required how can i disable the permission validation in this case?

Upvotes: 0

Views: 1310

Answers (2)

swapneel
swapneel

Reputation: 3061

I will suggest to use ViewModel which will have required fields to render on UI and may be specific to you Controller - Action.

see this SO link on best practices of using ViewModel -

Upvotes: 1

user1082916
user1082916

Reputation:

Use following in action.

[Bind(Exclude = "Permission")] 

To exclude multiple attribute, you can do

 [Bind(Exclude = "attribute1,attribute2,attribute3")] 

Upvotes: 0

Related Questions