强类型RadioButtonList MVC 3

发布于 2024-11-16 19:17:56 字数 2533 浏览 4 评论 0 原文

这可能吗?也许我很愚蠢,但我无法将此处或其他地方的任何答案应用于我的具体情况。

我想做的是将强类型的测验模型传递给我的视图。测验有一系列问题。问题有可能答案的集合。

问题看起来像:

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

MaybeAnswers 看起来像:

  • MaybeAnswer.Id
  • MaybeAnswer.Value
  • MaybeAnswer.Label
  • MaybeAnswer.Selected

我想将测验传递给视图,视图应该设置每个问题都有一个表单可能答案的radiobuttonlist(group)

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" />

现在我并不关心组中单选按钮的 id 的格式.. 显然 MVC 必须关心.. 所以无论哪种方式有效,我都想这样做。但我希望当我将表单提交给操作时,我应该能够循环遍历问题并在每个问题的 possibleAnswer 集合中获取可能的答案,并将 .isAnswer 标记为 true:

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

所以我不明白..这

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

    }

 }

是 :甚至还没有接近..有人可以解释一下我做错了什么吗?难道是我的模型不对? MVC 可以做到这一点吗?我是否需要将 MaybeAnswer 模型类型更改为 RadioButtonListItems 或其他类型?帮助!

- - - - - - - - - - - - - - - - - - ** 更新 ** - - - - - --------

@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);        
}

这段代码似乎可以工作!至少它会输出一些单选按钮..但它正在将 html 作为特殊实体输出..

<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>

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 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路黄昏 2024-11-23 19:17:56

这里是我使用的。我做了一些小的修改,但效果很好。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文