ASP.NET MVC 部分视图模型绑定
ASP.NET MVC Partiel view model binding
我是 ASP.NET MVC 的新手,我正在开发一个小测验应用程序项目。但是我遇到了一个相当烦人的问题。
现在我有几个模型,有问题、选择和测验。测验与表格相关联。我在表单中使用部分视图来显示每个问题,在问题中我使用另一个部分视图来显示选项。
每当我尝试从表单中获取哪些答案时,我得到 0 和 null。我已经尝试了很多东西,但是 none 都奏效了。
这是我的 类
选择
public class Choice
{
public int Id { get; set; }
public string Text { get; set; }
public bool IsAnswer { get; set; }
private Question question = new Question();
public bool IsSelected { get; set; }
public Question Question
{
get { return question; }
set { question = value; }
}
}
问题
public class Question
{
public int Id { get; set; }
private IList<Choice> choices = new List<Choice>();
public string Text { get; set; }
public int OrderNumber { get; set; }
public IList<Choice> Choices
{
get { return choices; }
set { choices = value; }
}
}
测验
public class Quiz
{
public int Id { get; set; }
public IList<Question> questions = new List<Question>();
public string Name { get; set; }
public IList<Question> Questions
{
get { return questions; }
set { questions = value; }
}
}
我的控制器。它总是将 'zzz' 设置为 null 和 0。
[HttpPost]
public ActionResult Grade(Quiz zzz)
{
var grade = quizService.Grade(zzz);
return View(grade);
}
观点。我以前没有 ViewDataDictionary,但它也给出了空值。我将其更改为 ViewDataDictionary,但它仍然返回空值。
注意:我现在确实添加了注释,它们不在实际代码中
发布
@model QuizApp.Models.Quiz
@{ ViewBag.Title = "Publish";}
@Html.Hidden("Id", Model.Id)
@using (Html.BeginForm("Grade","Quiz", FormMethod.Post))
{
foreach (var question in Model.Questions){
@Html.Partial("_Question", question, new ViewDataDictionary(){
TemplateInfo = new TemplateInfo()
{ HtmlFieldPrefix = "Questions" }
});
}
<input type="submit" value="Vesturen" />
}
问题
@model QuizApp.Models.Question
<div class="question">
@Html.Hidden("Quiz.Questions[" + Model.OrderNumber + "].Id", Model.Id)
//Shows the question
<b>@Model.Text </b><br/>
//Adds choices to the question
@foreach (var choice in Model.Choices)
{
@Html.Partial("_Choice", choice, new ViewDataDictionary()
{
TemplateInfo = new TemplateInfo()
{ HtmlFieldPrefix = "Choices" }
});
}
</div>
最后选择局部视图
@model QuizApp.Models.Choice
@Html.RadioButton("Quiz.Questions[" + Model.Question.OrderNumber + "].Choices[0].Id", Model.Id)
@Model.Text
有没有人看到我的问题的解决方案或解决方法?我做错了什么,我显然不知道吗?我不明白为什么它总是给我空值,因为据我所知它现在应该可以工作了。
以下将起作用:
for (var i = 0; i < Model.Questions.Count(); i++){
@Html.Partial("_Question", question, new ViewDataDictionary(){
TemplateInfo = new TemplateInfo()
{ HtmlFieldPrefix = "Questions[" + i.ToString() + "]" }
});
}
换句话说,您的字段前缀需要包含索引。此外,索引不能是任意值,正如您尝试使用 Model.Question.OrderNumber
所做的那样。它需要是列表中的实际索引。
或者,如果您按照以下行创建视图 Views\Shared\EditorTemplates\Question.cshtml
:
@model Namespace.To.Question
@Html.HiddenFor(m => m.Id)
...
那么,在你看来,你可以简单的做:
@Html.EditorFor(m => m.Questions)
并且 Razor 将为 Questions
中的每个项目呈现该编辑器模板,所有的前缀都正确。冲洗并与其他 类 一起重复,例如 Choice
,等等
我是 ASP.NET MVC 的新手,我正在开发一个小测验应用程序项目。但是我遇到了一个相当烦人的问题。
现在我有几个模型,有问题、选择和测验。测验与表格相关联。我在表单中使用部分视图来显示每个问题,在问题中我使用另一个部分视图来显示选项。
每当我尝试从表单中获取哪些答案时,我得到 0 和 null。我已经尝试了很多东西,但是 none 都奏效了。
这是我的 类
选择
public class Choice
{
public int Id { get; set; }
public string Text { get; set; }
public bool IsAnswer { get; set; }
private Question question = new Question();
public bool IsSelected { get; set; }
public Question Question
{
get { return question; }
set { question = value; }
}
}
问题
public class Question
{
public int Id { get; set; }
private IList<Choice> choices = new List<Choice>();
public string Text { get; set; }
public int OrderNumber { get; set; }
public IList<Choice> Choices
{
get { return choices; }
set { choices = value; }
}
}
测验
public class Quiz
{
public int Id { get; set; }
public IList<Question> questions = new List<Question>();
public string Name { get; set; }
public IList<Question> Questions
{
get { return questions; }
set { questions = value; }
}
}
我的控制器。它总是将 'zzz' 设置为 null 和 0。
[HttpPost]
public ActionResult Grade(Quiz zzz)
{
var grade = quizService.Grade(zzz);
return View(grade);
}
观点。我以前没有 ViewDataDictionary,但它也给出了空值。我将其更改为 ViewDataDictionary,但它仍然返回空值。
注意:我现在确实添加了注释,它们不在实际代码中
发布
@model QuizApp.Models.Quiz
@{ ViewBag.Title = "Publish";}
@Html.Hidden("Id", Model.Id)
@using (Html.BeginForm("Grade","Quiz", FormMethod.Post))
{
foreach (var question in Model.Questions){
@Html.Partial("_Question", question, new ViewDataDictionary(){
TemplateInfo = new TemplateInfo()
{ HtmlFieldPrefix = "Questions" }
});
}
<input type="submit" value="Vesturen" />
}
问题
@model QuizApp.Models.Question
<div class="question">
@Html.Hidden("Quiz.Questions[" + Model.OrderNumber + "].Id", Model.Id)
//Shows the question
<b>@Model.Text </b><br/>
//Adds choices to the question
@foreach (var choice in Model.Choices)
{
@Html.Partial("_Choice", choice, new ViewDataDictionary()
{
TemplateInfo = new TemplateInfo()
{ HtmlFieldPrefix = "Choices" }
});
}
</div>
最后选择局部视图
@model QuizApp.Models.Choice
@Html.RadioButton("Quiz.Questions[" + Model.Question.OrderNumber + "].Choices[0].Id", Model.Id)
@Model.Text
有没有人看到我的问题的解决方案或解决方法?我做错了什么,我显然不知道吗?我不明白为什么它总是给我空值,因为据我所知它现在应该可以工作了。
以下将起作用:
for (var i = 0; i < Model.Questions.Count(); i++){
@Html.Partial("_Question", question, new ViewDataDictionary(){
TemplateInfo = new TemplateInfo()
{ HtmlFieldPrefix = "Questions[" + i.ToString() + "]" }
});
}
换句话说,您的字段前缀需要包含索引。此外,索引不能是任意值,正如您尝试使用 Model.Question.OrderNumber
所做的那样。它需要是列表中的实际索引。
或者,如果您按照以下行创建视图 Views\Shared\EditorTemplates\Question.cshtml
:
@model Namespace.To.Question
@Html.HiddenFor(m => m.Id)
...
那么,在你看来,你可以简单的做:
@Html.EditorFor(m => m.Questions)
并且 Razor 将为 Questions
中的每个项目呈现该编辑器模板,所有的前缀都正确。冲洗并与其他 类 一起重复,例如 Choice
,等等