Reputation: 347
I have a view model that contains two models, one is a IList:
public class PersonViewModel
{
public Person person { get; set; }
public IList<Snack> { get; set; }
}
This outputs all of the data I need for my main view and partial view just fine. The problem I'm having is when I hit my action that is tied to the submit button.
@using (Html.BeginForm("ProcessSnacks", "Person", FormMethod.Post))
{
<input id="process" type="submit" value="Process Snacks" />
}
On my controller I have:
public ActionResult ProcessSnacks(PersonViewModel vm)
{
//ViewModel is NULL here...
}
I actually just need the data from the Person model inside the PersonViewModel and have tried many permutations... I've tried just having person as the parameter instead of the view model but no luck. I've been all over StackOverflow as well and can't seem to find another issue that quite matches.
Upvotes: 0
Views: 84
Reputation: 51
Put in some textforms/dropdowns or hiddentextboxes on the view with your model as the values, you should then see it on the post back.
As it stands your view doesn't contain any model values hence on postback it will be blank.
Upvotes: 0
Reputation: 13600
You just tied viewmodel to the view on the server-side, but the view itself doesn't use the data in any way, so when you submit it, client-side can't possibly know that you want the some data posted back to the server. If you want such behaviour, you need to have some fields inside your form, that would make it possible to send the data back to the server.
You must understand, posting back in MVC isn't some miracle, most work is done for you though (binding for instance), but you have to provide bare minimum for it to work. If you don't want to have a form with textboxes, you can use for instance @Html.HiddenFor(...)
helper for all fields you need to post to server.
Please note that even though hidden fields aren't visible in browser, they are visible and editable (if you know how), so it is not safe to pass some data that need to be protected from the user.
Upvotes: 4