0
votes

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 ActionResult Edit(Dinner dinner) {

        //should this dinner class be populated?

    }
1
can you add your View code as well? You do understand when you return View(dinner) it is going to the View and not the Edit Controller - neebz
I think it's best explained in scottGu's own sample on his blog Look at part 5 - Guidhouse
Hmm..., my view doesn't manipulate any of the objects property. It adds few property for ViewBag.Guests collection. Looks like I need I need to put the values as HIDDEN INPUT for dinner property? - Nil Pun
Yea. You need to keep them in hidden inputs. Thats how HTTP statelessness works. Once you send the dinner object to the view. To return it you have to keep the state in the controls. - neebz

1 Answers

0
votes

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.