开发者

MVC form post Question

开发者 https://www.devze.com 2023-02-18 23:10 出处:网络
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?

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.

0

精彩评论

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