MVC2 FormCollection options
I'm just getting started with MVC2 and going through the NerdDinner examples. I noticed that there seems to be multip开发者_如何学Gole ways to pass in the form values for example:
FormColelction formvalues
FormCollection collection
FormCollection form
Why would you use one over the other and why? Does it also relate to whether you are using Entity Framework?
Each of the above examples is passing in the same type (FormCollection) but with just a different variable name.
It would have been nice to see some consistency in NerdDinner.
You can also have MVC automagically populate a custom object from your form instead of using the generic FormCollection.
EG If your form being posted has fields for FirstName, LastName and Age (representing a person) you could have a method like
[HttpPost]
public ActionResult Create(Person person)
{
//person is already populated
}
[HttpPost]
public ActionResult Create([Bind(Exclude = "id")]Person post_person)
{
// post_person => auto populate formpost values
// [Bind(Exclude = "id")] => excluding auto populate identity field
}
精彩评论