0

I have a question regarding Binding of attributes in a Controller's Action.

My model has 3 attributes: Name, UserName and Email:

public class User {
    public string Name {get;set;}
    public string UserName {get;set;}
    public string Email {get;set;}
}

If I want my Create Action to have a new attribute, such as a "Login" Boolean, is there a way to Bind a new attribute to my model, like:

public ActionResult Create([Bind(Include = "Name, UserName, Email, Login")] User model)

Or do I must create a ViewModel to do so?

Vitor Durante
  • 950
  • 8
  • 25
  • 2
    Create a ViewModel, that's what they are for! – zgood Feb 03 '16 at 20:07
  • By default all properties are bound, so its only necessary to use `[Bind(Include="..")]` (or `Exclude`) if your want to prevent some properties from binding. But the correct approach is to use a view model –  Feb 03 '16 at 20:56

1 Answers1

0

You can create a viewmodel with all those 4 properties as use that. That is the best way to prevent overposting.

But if you do not want to create a view model. you can definitely add new parameters to your action method. The name of the parameter should match to your corresponding form element name.

public ActionResult Create([Bind(Include = "Name, UserName, Email")] User model,bool Login)
{
   //to do : return something
}
Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497