在ASP MVC 3中获取单选按钮值的列表 [英] Getting a list of radio button values in ASP MVC 3

查看:155
本文介绍了在ASP MVC 3中获取单选按钮值的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个有关评分问题的页面.

I am developing a page for rating questions.

在视图中,我有一个问题列表和每个问题前面的5个单选按钮.

In the view, I have a list of questions and 5 radio buttons in front of each one of them.

<input name="evalId" type="hidden" value="@Model.Evaluation.EvalId" />
foreach (var question in questionList)
{
     <input name="questionId" type="hidden" value="@question.QuestionId" />
     <div class="row_star" style="border-bottom : 0 none; background: none;">
       @if (!String.IsNullOrEmpty(question.QuestionTitre))
       {
         <p>@question.QuestionTitre.TrimEnd()</p>
       }

       @* here goes the code for 5 radio buttons*@
}

现在,在我的控制器中,我希望能够知道为每个问题选中了哪个单选按钮.

Now, in my controller I want to be able to know which radio button was checked for each question.

我该怎么做?

这是我的ViewModel

Here is my ViewModel

public class EvaluationViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public EvalEvaluation Evaluation
    {
        get;
        set;
    }

    /// <summary>
    /// 
    /// </summary>
    public Dictionary<EvalQuizz, List<EvalQuestion>> EvalQuizzQuestionList
    {
        get;
        set;
    }
}

推荐答案

假设您的ViewModel是这样的

Assuming your ViewModel is like this

public class Question
{
    public int ID { set; get; }
    public string QuestionText { set; get; }
    public List<Answer> Answers { set; get; }
    public int SelectedAnswer { set; get; }
    public Question()
    {
        Answers = new List<Answer>();
    }
}
public class Answer
{
    public int ID { set; get; }
    public string AnswerText { set; get; }
}
public class Evaluation
{
    public List<Question> Questions { set; get; }
    public Evaluation()
    {
        Questions = new List<Question>();
    }
}

在GET操作方法中,您将把视图模型返回到视图,并在其中填充了一些问题和答案.在下面的代码中,我已经对问题和答案进行了硬编码.您可以从存储库/服务层中获取它.

And in your GET action method, you will return the viewmodel back to the view with some questions and answers filled in it. In the code below I've hardcoded the questions and answers. You may get it from your Repositary/Service layer.

public ActionResult Index()
{
    var evalVM = new Evaluation();

    //the below is hardcoded for DEMO. you may get the data from some  
    //other place and set the questions and answers

    var q1=new Question { ID=1, QuestionText="What is your favourite language"};
    q1.Answers.Add(new Answer{ ID=12, AnswerText="PHP"});
    q1.Answers.Add(new Answer{ ID=13, AnswerText="ASP.NET"});
    q1.Answers.Add(new Answer { ID = 14, AnswerText = "Java" });
    evalVM.Questions.Add(q1);

    var q2=new Question { ID=2, QuestionText="What is your favourite DB"};
    q2.Answers.Add(new Answer{ ID=16, AnswerText="SQL Server"});
    q2.Answers.Add(new Answer{ ID=17, AnswerText="MySQL"});
    q2.Answers.Add(new Answer { ID=18, AnswerText = "Oracle" });
    evalVM.Questions.Add(q2);

    return View(evalVM);
}

现在,我们将创建一个编辑器模板来呈现我们的问题.因此请转到查看文件夹",并在具有当前控制器名称的文件夹下创建一个名为 EditorTemplates 的文件夹. 将视图添加到EditorTemplates文件夹,并提供与我们要表示的类名称相同的名称.即:Question.cshtml

Now we will create an Editor Template to render our Question. so go to your View Folder and create a folder called EditorTemplates under the folder with your current controller name. Add a view to the EditorTemplates folder and give the same name as the class name we want to represent. ie : Question.cshtml

现在将这段代码放在编辑器模板中

Now put this code in the editor tempalte

@model YourNameSpace.Question
<div>
    @Html.HiddenFor(x=>x.ID)
    @Model.QuestionText
    @foreach (var a in Model.Answers)
    {
        <p>
        @Html.RadioButtonFor(b=>b.SelectedAnswer,a.ID)  @a.AnswerText 
        </p>
    }
</div>

现在转到主视图,并使用EditorTemplate html helper方法将我们创建的EditorTemplate带到主视图.

Now go to our main view and use EditorTemplate html helper method to bring the EditorTemplate we created to the main view.

@model YourNameSpace.Evaluation
<h2>Index</h2>
@using (Html.BeginForm())
{
    @Html.EditorFor(x=>x.Questions)
    <input type="submit" />
}

现在在HttpPost中,您可以检查发布的模型并在那里获取选定的单选按钮(SelectedAnswer)值

Now in your HttpPost you can check the posted model and get the selected radio button (SelectedAnswer) value there

[HttpPost]
public ActionResult Index(Evaluation model)
{
    if (ModelState.IsValid)
    {
        foreach (var q in model.Questions)
        {
            var qId = q.ID;
            var selectedAnswer = q.SelectedAnswer;
            //Save

        }
        return RedirectToAction("ThankYou"); //PRG Pattern
    }
    //reload questions
    return View(model);
}

如果使用Visual Studio断点,则可以看到发布的值.多亏了MVC模型绑定:)

If you use visual studio breakpoints, you can see the values posted. Thanks to MVC Model binding :)

您可以阅读并在此处下载工作示例.

这篇关于在ASP MVC 3中获取单选按钮值的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆