2

I have the followingsimple models:

public class MainModel
{
    public int Id {get;set;}
    public List<Question> Questions {get;set;}
}

public class Question
{
    public string Text {get;set;}
    public List<Answer> Answers {get;set;}
}

public class Answer
{
    public byte No {get;set;}
    public string Text {get;set;}
    public bool Correct {get;set;}
}

I have strongly typed view to MainModel which allows users to add custom number of questions and answers for each question (and remove them as well).

I'm using the method with adding hidden indexing field and it works ok for the Question level (dynamically added questions are being included on POST). However, when it comes to dynamically added answers, it's not working properly. This is the HTML I'm having rendered as a result:

<div class="answer">
<input type="hidden" value="1" name="Questions[2].Answers.Index">
<input type="checkbox" data-checkbox-for="Questions[2].Answers[1].Correct" checked="checked">
<input type="hidden" value="1" name="Questions[2].Answers[1].No">
<input type="text" value="2.1" name="Questions[2].Answers[1].Text">
<input type="hidden" value="true" name="Questions[2].Answers[1].Correct">
<span class="remove-answer link">Remove</span>
</div>

What am I doing wrong here?

2 Answers 2

1

Do you start with zero index?

 <div class="answer">
<input type="hidden" value="1" name="Questions[2].Answers.Index">
<input type="checkbox" data-checkbox-for="Questions[2].Answers[0].Correct" checked="checked">
<input type="hidden" value="1" name="Questions[2].Answers[0].No">
<input type="text" value="2.1" name="Questions[2].Answers[0].Text">
<input type="hidden" value="true" name="Questions[2].Answers[0].Correct">
<span class="remove-answer link">Remove</span> 

if you don't start counting from 0, you will get no post values, because mvc wants index 0 before index 1

and following property is not in your model, is it?

<input type="hidden" value="1" name="Questions[2].Answers.Index">
Sign up to request clarification or add additional context in comments.

2 Comments

That property is used to bypass requirement for sequential counting. Meaning I can have indexes 1, 5, 90... as long as I have that row, they will get bound on server-side as 1,2,3. I believe I've found issue, seems what I wrote above is actually working - error was somewhere else.
Barisa: Are you using a custom binder of some sort? Because, at least in MVC3, the default model binders will ignore any array indexes the moment it finds a gap in the numbering(ie, if you have [0, 1, 3], you'll never see 3).
0

Here's the way to go over this:

Model Binding To List Of Objects In ASP.NET MVC

Make sure you add a hidden index with some sort of randomness. This way you can have "gaps" in between various index values.

Something like this:

var random = new Random(10);
var rnd = random.Next();

@Html.Hidden("Questions[" + (i) + "].Answers.Index", i + j + rnd)
@Html.TextBox("Questions[" + (i) + "].Answers[" + (i + j + rnd) + "].Text", Model.Questions[i].Answers[j].Text, new { @class = "form-control" })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.