简体   繁体   English

ASP.NET MVC多个单选按钮表单

[英]ASP.NET MVC Multiple Radiobutton Forms

I am working on an ASP.NET MVC application which allows users to create their own surveys. 我正在使用ASP.NET MVC应用程序,该应用程序允许用户创建自己的调查表。 For the sake of simplicity in answering this question, lets say that they can only create surveys with multiple-choice questions, each of which will be displayed to a survey-taker as a question followed by a list of radiobuttons representing each possible answer. 为了简化回答这个问题,可以说他们只能创建带有多项选择题的调查问卷,每个问题都会作为一个问题显示给调查员,然后是代表每个可能答案的单选按钮列表。 Users can create as many multiple-choice questions in a survey as they want. 用户可以根据需要在调查中创建尽可能多的多项选择题。

So, I have a ViewModel as follows: 因此,我有一个ViewModel如下:

public class SurveyViewModel
{
    public int Id { get; set; }
    public IList<Question> Questions { get; set; }
}

public class Question
{
    public int Id { get; set; }
    public string Question { get; set; }
    public IList<Answer> Answers { get; set; }
}

public class Answer
{
    public int Id { get; set; }
    public string LabelText { get; set; }
}

Usually, I would just add a field for each question in the form to the SurveyViewModel . 通常,我只是将每个问题的字段以表格的形式添加到SurveyViewModel However, in this case I obviously don't know how many questions exist because the user can create however many they want - so I can't create a field to store the user's answer for each question. 但是,在这种情况下,我显然不知道存在多少问题,因为用户可以创建任意数量的问题-因此我无法创建一个字段来存储用户对每个问题的答案。

So my question is: How can I write the ViewModel and the View (in Razor syntax) so that I can submit a form to a Controller, such that the responses to each of the arbitrary number of multiple-choice questions can be saved? 所以我的问题是:如何编写ViewModel和View(以Razor语法),以便可以向Controller提交表单,从而可以保存对任意数量的多项选择题的回答?

I have spent a long time banging my head against the wall on this one, so all help is much appreciated! 我花了很长的时间在这个墙上敲头,所以非常感谢所有帮助! Thank you! 谢谢!

Not sure exactly what you're asking because it seems you have everything you need, or at least most of what you need. 不确定您要问的是什么,因为您似乎拥有了所需的一切,或者至少满足了大部分需求。 In your view, you would just iterate over Questions on your view model, and then for each question, iterate over Answers . 在您的视图中,您只需遍历视图模型上的Questions ,然后针对每个问题遍历Answers The only thing to bear in mind is that you need to use a for loop rather than foreach so you can index the list properties, allowing Razor to generate the right input names for the modelbinder. 唯一要记住的是,您需要使用for循环而不是foreach以便可以对列表属性建立索引,从而使Razor可以为modelbinder生成正确的输入名称。 The one modification you need to make is to add a property like the following to Question : 您需要进行的一项修改是向Question添加以下属性:

public int SelectedAnswerId { get; set; }

Then, your view would look something like: 然后,您的视图将类似于:

@for (var i = 0; i < Model.Questions.Count(); i++)
{
    <p>@Model.Questions[i].Question</p>

    @for (var j = 0; j < Model.Questions[i].Answers.Count(); j++)
    {
        @Html.RadioButtonFor(m => m.Questions[i].SelectedAnswerId, Model.Questions[i].Answers[j].Id, new { id = "Question" + i.ToString() + "Answer" + j.ToString() })
        <label for="Question@(i)Answer@(j)">@Model.Questions[i].Answers[j].LabelText</label>
    }
}

EDIT : Corrected radio button labels by passing an id for each each radio button and creating a label using that id. 编辑 :通过传递每个单选按钮的ID并使用该ID创建标签来更正单选按钮标签。 Otherwise, you'd end up with a bunch of labels that wouldn't actually activate the appropriate radio button. 否则,您将得到一堆标签,这些标签实际上不会激活相应的单选按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM