Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have a look at the input names that are being generated within your HTML. I think you have an issue with the naming of your controls thus the default model binding is failing since you mentioned that the using <code>FormCollection</code> works correctly. I am making this assumption since I don't know what your Questions and Answers classes look like</p> <pre><code>&lt;%= Html.TextBoxFor(model =&gt; model.Answers[(int)question.QuestionID - 1].AnswerValue) %&gt; </code></pre> <p>Won't this render something similar to the following; which if I'm not mistaken will not bind to your model. The same applies for the <code>RadioButtons</code>.</p> <pre><code>&lt;input type="text" name="Answers[0].AnswerValue" id="Answers_0__AnswerValue" value="somevalues"/&gt; </code></pre> <p>The RadioButton helper should be</p> <pre><code>&lt;%= Html.RadioButton("Questions[" + question.QuestionID + "].ID", "Yes", false)%&gt; // you now get a list of questions &lt;input type="radio" name="Questions[1].ID" id="Questions_1__ID" value="No" checked="checked"/&gt; </code></pre> <p>There are some ways you can try to resolve this:</p> <ol> <li>Correctly name your input controls so that they match you ViewModel. (This includes posting back all the required fields for you model - I think that that default values are used when the model binding occurs if its not posted)</li> <li>Create a custom model binder</li> <li>You may need to tell the Binder what the Prefix of the input fields are. (<a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.bindattribute_members.aspx" rel="nofollow noreferrer"><code>[Bind]</code> attribute</a>) to specifically include or exclude form fields.</li> <li>Create a new Model containing the values you expect to post back</li> </ol> <p>I think that overall that your approach needs to slightly change. From the info provided, your Q&amp;As are closely related. Depending on the question type your 'answer' is either boolean or freetext. At the moment your are not posting a <code>List&lt;Questions&gt;</code> back to the server. Answers, yes, but they are not I don't think that it is recognised as <code>List&lt;Answers&gt;</code>. </p> <p><a href="http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx" rel="nofollow noreferrer">Haacked</a> has a post which I think is related to your issue and <a href="https://stackoverflow.com/questions/2293025/asp-net-mvc-2-0-ilistt-checkboxes">this SO question</a> further indicates that it may still applly to ASP-MVC-2.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload