MVC form post Question
I'm newbie to MVC and my FORM Post is not working. Could anyone tell me if I pass Dinner object to the VIEW. And do HTTP POST, should MVC give Person object back? e.g. public ActionResult Edit(int id) {
Dinner dinner = dinnerRepository.GetDinner(id);
if (!dinner.IsHostedBy(User.Identity.Name))
return View("InvalidOwner");
return View(dinner);
}
public Ac开发者_如何学JAVAtionResult Edit(Dinner dinner) {
//should this dinner class be populated?
}
The default model binder automatically populates action arguments if values are present in the request. So for example if your form contains the following fields:
<input type="text" name="Foo" />
<input type="text" name="Bar" />
and your Dinner object contains those properties:
public class Dinner
{
public string Foo { get; set; }
public string Bar { get; set; }
}
then when you submit the form to the following action:
[HttpPost]
public ActionResult Edit(Dinner dinner)
{
// the dinner.Foo and dinner.Bar properties will be
// automatically bound from the request
...
}
For more advanced binding scenarios such as lists and dictionaries you may checkout the following blog post for the correct wire format.
精彩评论