Search code examples
asp.net-mvcasp.net-mvc-3postcontrollerstrong-typing

Postback values getting lost!


I have a controller with typical create methods (one for GET, one for POST). the POST takes a strongly-typed parameter:

[HttpPost] public ActionResult Create(Quiz entity)

however, when the callback is made, the properties of my entity are null... if I redefine it like this:

[HttpPost] public ActionResult Create(Quiz entity, FormCollection form)

I can see that the values are there e.g. form["Component"] contains "1". I've not had this problem in the past and I can't figure out why this class would be different.

thoughts anyone?


Solution

  • I FIGURED IT OUT!!

    so, in my model (see comments on @ataddeini's thread below) you can see I have a Component... to represent components I used a couple of listboxes, the second (Components) dependent on the contents of the first (Products). In generating the second list I used

    @Html.DropDownListFor(x => x.Component, ...)
    

    which (as shown in one of the above links) generates a form field called "Component"... and therein lies the problem. What I needed to have done is bind it to the the Id of the component instead!

    @Html.DropDowListFor(x => x.Component.Id, ...)
    

    hurray!