开发者

Strongly Type RadioButtonList MVC 3

Is this even possible? Maybe i am stupid but i cannot apply any of the answers here or elsewhere to my specific situation.

What i want to do is pass a strongly typed model of Quiz to 开发者_Go百科my view. Quiz has a collections of questions. Question has a collection of possibleanswers.

Questions look like:

  • Question.Text
  • Question.Id
  • Question.PossibleAnswers

PossibleAnswers look like:

  • PossibleAnswer.Id
  • PossibleAnswer.Value
  • PossibleAnswer.Label
  • PossibleAnswer.Selected

I want to pass the Quiz to the view, the view should setup the form where each Question has a radiobuttonlist(group) of PossibleAnswers.

Question1 text blah blah blah:

<input id="1_1" type="radio" name="question1"  value="1" />
<input id="1_2" type="radio" name="question1"  value="2" />
<input id="1_3" type="radio" name="question1"  value="3" />

Now i don't care exactly the format for the id's of the radio buttons in the group.. clearly MVC must care.. so whatever way works i want to do that. But i would expect when i submit the form to the action i should be able to loop through the Questions and get the possibleAnswer in each Question's possibleAnswer collections with the .isAnswer marked as true:

foreach(var item in model.Questions)
{
    foreach(var aitem in item.PossibleAnswers)
    {
        if(aitem.Selected)
           //do some stuff
    }
}

so i don't get it.. this:

@foreach (var item in Model.Questions)
{
    @foreach (var jitem in item.PossibleAnswers)
    {
        @Html.RadioButtonFor(x => item.QuestionId, jitem.Value)

    }

 }

is not even close.. can someone please explain what i am doing wrong? Is my model wrong? Can MVC do this? Do i need to change the PossibleAnswer model type to RadioButtonListItems or something? help!

------------------------------------ ** UPDATE ** ----------------

@for (int i = 0; i < Model.Questions.Count(); i++)
{      
    <p>@Model.Questions.ElementAt(i).QuestionText</p>
    @Html.RadioButtonListFor(m => m.Questions.ElementAt(i).AnswerRadios,
        "Question" + i);        
}

This code seems to work! At least it will output some radiobuttons.. BUT it is outputting the html as special entities..

<td id="Question0_Container">

    <input id="Question0_1" name="Question0_value" type="radio" value="17" />
    <label for="Question0_1">1</label>

    <input id="Question0_2" name="Question0_value" type="radio" value="18" />
    <label for="Question0_2">2</label>

    <input id="Question0_3" name="Question0_value" type="radio" value="19" />
    <label for="Question0_3">3</label>

    <input id="Question0_4" name="Question0_value" type="radio" value="20" />
    <label for="Question0_4">4</label>

    <input id="Question0_5" name="Question0_value" type="radio" value="21" />
    <label for="Question0_5">5</label>

</td>


Here is what I use. I have made some minor modifications but it works great.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜