C# Asp.net MVC Razor如何显示两个模型字段的分组单选按钮
我有一个简单的测验模型,我试图让用户在强类型视图中从两个单选按钮(分组)中选择正确答案/备选答案。但是我使用的lambda表达式不起作用。我有两个空白单选按钮。我在这里和网上研究了几个问题,但我的模型是IList,我找不到合适的例子。我发现的所有示例都适用于非IList 这是我的模型 型号:C# Asp.net MVC Razor如何显示两个模型字段的分组单选按钮,c#,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,razor,C#,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,Razor,我有一个简单的测验模型,我试图让用户在强类型视图中从两个单选按钮(分组)中选择正确答案/备选答案。但是我使用的lambda表达式不起作用。我有两个空白单选按钮。我在这里和网上研究了几个问题,但我的模型是IList,我找不到合适的例子。我发现的所有示例都适用于非IList 这是我的模型 型号: public partial class Question { public int QuestionID { get; set; } public string Q
public partial class Question
{
public int QuestionID { get; set; }
public string QuestionBody { get; set; }
public string CorrectAnswer { get; set; }
public string AlternativeAnswer { get; set; }
}
@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
}
我的控制器
public ActionResult Index()
{
QuizSimpleEntities quizEntities = new QuizSimpleEntities();
var questions = from p in quizEntities.Questions
select p;
return View(questions.ToList());
}
我的型号:
public partial class Question
{
public int QuestionID { get; set; }
public string QuestionBody { get; set; }
public string CorrectAnswer { get; set; }
public string AlternativeAnswer { get; set; }
}
@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
}
@model-IList
欢迎参加测验
@BeginForm(方法:FormMethod.Post,controllerName:“Home”,actionName:“index”)
{
@foreach(模型中的var问题)
{
@问题。问题正文
@*如何显示正确答案和备选答案
作为两个单选按钮分组在这里?我将张贴所选的值回来
}
}
谢谢您需要在视图模型上设置一个属性,以便在发布表单时保存所选答案:
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; }
}
然后简单地循环模型的元素并生成所需的标记:
@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>
}
@model-IList
欢迎参加测验
@BeginForm(方法:FormMethod.Post,controllerName:“Home”,actionName:“index”)
{
@对于(var i=0;ix[i].QuestionID)
@DisplayFor(x=>x[i].QuestionBody)
-
@Html.HiddenFor(x=>x[i].CorrectAnswer)
@Html.RadioButton(x=>x[i]。选择回答,型号[i]。正确答案)
@DisplayFor(x=>x[i].CorrectAnswer)
-
@Html.HiddenFor(x=>x[i].AlternativeAnswer)
@Html.RadioButton(x=>x[i]。选择回答,模型[i]。备选回答)
@DisplayFor(x=>x[i].AlternativeAnswer)
}
好啊
}
注意:提交表单时,POST操作可能会采用
IList
模型,在该模型中,您将获得每个问题的答案(在SelectedAnswer
属性中)。我不明白您为每个单选按钮添加的隐藏字段的用途。有记录吗?