Asp.net MVC剃刀如何显示分组的单选按钮有两个示范田 [英] Asp.net MVC Razor how to show grouped radio buttons for two model fields

查看:87
本文介绍了Asp.net MVC剃刀如何显示分组的单选按钮有两个示范田的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的测验模型,我试图让用户从两个单选按钮,分组,在一个强类型的视图中选择正确答案/备选答案。但是我用的是拉姆达前pressions不工作。我得到两个空白的单选按钮。我在几个问题看这里,并且在线,但我的模型是一个IList<>,我不能找到一个合适的例子。我所有的例子都找到了工作与非的IList<>

I have a simple quiz model, and I am trying to let the user select Correct Answer/Alternative answer from two radio buttons, grouped , in a strongly typed view. But the lambda expressions I use aren't working. I get two blank radio buttons. I have looked at several questions here, and online but my model is an IList<>, and I can't find a suitable example. All examples I found work with a non-IList<>.

这是我的模型

型号:

public partial class Question
    {
        public int QuestionID { get; set; }
        public string QuestionBody { get; set; }
        public string CorrectAnswer { get; set; }
        public string AlternativeAnswer { get; set; }           
    }

我的控制器

public ActionResult Index()
        {
            QuizSimpleEntities quizEntities = new QuizSimpleEntities();
            var questions = from p in quizEntities.Questions
                            select p;

            return View(questions.ToList());

        }

我的型号:

  @model IList<Quiz.Models.Question>                                

 <h2>Welcome to the Quiz</h2>
  @Html.BeginForm(method:FormMethod.Post,controllerName:"Home",actionName:"index")
    {
        @foreach (var questions in Model)
        {

        <p>@questions.QuestionBody</p>  

        @* How to display the CorrectAnswer and AlternativeAnswer
           as two radio buttons grouped here? I will be posting the selected value back
        }

}

感谢您

推荐答案

您必须对您的视图模型的属性,将举行当表单张贴的选择答案:

You need to have a property on your view model that will hold the selected answer when the form is posted:

public partial class Question
{
    public int QuestionID { get; set; }
    public string QuestionBody { get; set; }
    public string CorrectAnswer { get; set; }
    public string AlternativeAnswer { get; set; }           

    public string SelectedAnswer { get; set; }
}

,然后简单地通过你的模型的元素循环,并生成所需的标记:

and then simply loop through the elements of your model and generated the desired markup:

@model IList<Quiz.Models.Question>                                

<h2>Welcome to the Quiz</h2>
@Html.BeginForm( method:FormMethod.Post, controllerName:"Home", actionName:"index")
{
    @for (var i = 0; i < Model.Count; i++)
    {
        @Html.HiddenFor(x => x[i].QuestionID)
        <fieldset>
            <legend>
                @Html.DisplayFor(x => x[i].QuestionBody)
            </legend>
            <ul>
                <li>
                    @Html.HiddenFor(x => x[i].CorrectAnswer)
                    @Html.RadioButtonFor(x => x[i].SelectedAnswer, Model[i].CorrectAnswer)
                    @Html.DisplayFor(x => x[i].CorrectAnswer)
                </li>
                <li>
                    @Html.HiddenFor(x => x[i].AlternativeAnswer)
                    @Html.RadioButtonFor(x => x[i].SelectedAnswer, Model[i].AlternativeAnswer)
                    @Html.DisplayFor(x => x[i].AlternativeAnswer)
                </li>
            </ul>
        </fieldset>
    }

    <button type="submit">OK</button>
}

请注意:当表单提交的POST操作可能需要一个的IList&LT;问题&GT; 模型中,你会为每一个问题的答案(在 SelectedAnswer 属性)。

NOTE: When the form is submitted the POST action could take an IList<Question> model where you will have the answers for each question (in the SelectedAnswer property).

这篇关于Asp.net MVC剃刀如何显示分组的单选按钮有两个示范田的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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