发送列表以查看,填写并在 asp.net MVC中获取
本文关键字:asp net MVC 获取 列表 | 更新日期: 2023-09-27 17:56:10
我有一个测验表格,它显示一些带有答案的问题(单选按钮),用户必须回答问题并发送,我做了这样的课程:
public class Question
{
public int Qid { get; set; }
public string Questionstext { get; set; }
public int selected { get; set; }
public List<Qitem> lst { get; set; }
}
public class Qitem
{
public int id { get; set; }
public string txt { get; set; }
}
然后我发送一个问题列表到视图中:
List<Question> llst = new List<Question>
{
new Question
{
Qid = 1,
Questionstext = "is it true?",
lst = new List<Qitem>
{
new Qitem
{
txt = "yes",
id = 1
},
new Qitem
{
id = 2,
txt = "no"
}
}
},
new Question
{
Qid = 1,
Questionstext = "is it true 2?",
lst = new List<Qitem>
{
new Qitem
{
txt = "yes2",
id = 3
},
new Qitem
{
id = 4,
txt = "yes3"
}
}
}
};
return View(llst);
我如何显示它然后提交答案,答案必须放在问题的"选定"属性中。我的问题是关于视图和特别是单选按钮。
您的观点应该是
@model List<Question>
@using (Html.BeginForm())
{
for (int i = 0; i < Model.Count; i++)
{
@Html.HiddenFor(m => m[i].Qid)
<h2>@Model[i].Questionstext</h2>
foreach (var answer in Model[i].lst)
{
<label>
@Html.RadioButtonFor(m => m[i].selected, answer.id, new { id = "" })
<span>@answer.txt </span>
</label>
}
}
<input type="submit" value="Save" />
}
它将回发到(假设Index
与用于生成此视图的 GET 方法同名)。
public ActionResult Index(List<Question> model)
旁注:如果还希望过帐Questionstext
值,请包括@Html.HiddenFor(m => m[i].Questionstext)