MVC 提交时在控制器中返回空对象
MVC on submit getting null object back in controller
我将 Razor 与 MVC 5、C# 和一个与另一个模型 (ClassTestQuestionMc) 相关的模型 (ClassTestQuestion) 一起使用。
我选中了一些复选框,当我在控制器中按下提交按钮(完成)时,我得到了空对象。
我怎样才能取回结果?
在视图中:
@model IEnumerable<OnlineLearningMVC.Models.ClassTestQuestion>
@using (Html.BeginForm("FinishTest", "ClassTestQuestions", FormMethod.Post))
{
@Html.AntiForgeryToken()
foreach (var item in Model)
{
@Html.DisplayFor(model => item.QuestionTx)
@Html.HiddenFor(model => item.Id)
@Html.HiddenFor(model => item.QuestionTx)
<br/>
<br />
foreach (var Question in item.ClassTestQuestionMc)
{
@Html.DisplayFor(model => Question.AnswerTx)
@Html.HiddenFor(model => Question.AnswerTx)
@Html.CheckBoxFor(model => Question.IsChecked)
@Html.HiddenFor(model => Question.IsChecked)
}
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="FinishTest" class="btn btn-default" />
</div>
</div>
在控制器中:
public ActionResult ClassCourseTest(int IdCourse)
{
var classTestQuestions = db.ClassTestQuestions.Include(c=>c.ClassTestQuestionMc).Include(c => c.ClassTest).Where(i=>i.ClassTestId== IdCourse);
return View("ClassCourseTest", classTestQuestions.ToList());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult FinishTest(ClassTestQuestion classTestQuestion)
{
return View(classTestQuestion);
}
我的课堂测试问题模型:
namespace OnlineLearningMVC.Models
{
public class ClassTestQuestion
{
public int Id { set; get; }
public int ClassTestId { set; get; }
public virtual ClassTest ClassTest { set; get; }
[Required]
[DisplayName("Question")]
public string QuestionTx { set; get; }
[Required]
[DisplayName("Order")]
public int OrderInt { get; set; }
[DisplayName("Disabled")]
public bool IsDeleted { set; get; }
public string CreatedFrom { set; get; }
public DateTime CreatedDate { set; get; }
public string UpdatedFrom { set; get; }
public DateTime UpdatedDate { set; get; }
public virtual ICollection<ClassTestQuestionMc> ClassTestQuestionMc { set; get; }
}
我的 ClassTestQuestionMc 模型:
namespace OnlineLearningMVC.Models
{
public class ClassTestQuestionMc
{
public int Id { set; get; }
public int ClassTestQuestionId { set; get; }
public virtual ClassTestQuestion ClassTestQuestion { set; get; }
[Required]
public string AnswerTx { set; get; }
[DisplayName("Is Correct Answer?")]
public bool IsCorrectAnswer { set; get; }
public bool IsChecked { set; get; }
[DisplayName("Disabled")]
public bool IsDeleted { set; get; }
public string CreatedFrom { set; get; }
public DateTime CreatedDate { set; get; }
public string UpdatedFrom { set; get; }
public DateTime UpdatedDate { set; get; }
}
我在浏览器中看到的:
编辑
我尝试更改为 IEnumerable :
将 ClassTestQuestionMc 的 ICollection 更改为 IList。然后你需要在这里遵循数组名称约定,使 MVC 能够处理视图模型。
foreach (int i = 0; i < Model.Count; i++)
{
@Html.DisplayFor(model => Model[i].QuestionTx)
@Html.HiddenFor(model => Model[i].Id)
@Html.HiddenFor(model => Model[i].QuestionTx)
<br/>
<br/>
foreach (int j = 0; j < Model[i].ClassTestQuestionMc; j++)
{
@Html.DisplayFor(model => Model[i].ClassTestQuestionMc[j].AnswerTx)
@Html.HiddenFor(model => Model[i].ClassTestQuestionMc[j].AnswerTx)
@Html.CheckBoxFor(model => Model[i].ClassTestQuestionMc[j].IsChecked)
@Html.HiddenFor(model => Model[i].ClassTestQuestionMc[j].IsChecked)
}
}
我将 Razor 与 MVC 5、C# 和一个与另一个模型 (ClassTestQuestionMc) 相关的模型 (ClassTestQuestion) 一起使用。
我选中了一些复选框,当我在控制器中按下提交按钮(完成)时,我得到了空对象。 我怎样才能取回结果?
在视图中:
@model IEnumerable<OnlineLearningMVC.Models.ClassTestQuestion>
@using (Html.BeginForm("FinishTest", "ClassTestQuestions", FormMethod.Post))
{
@Html.AntiForgeryToken()
foreach (var item in Model)
{
@Html.DisplayFor(model => item.QuestionTx)
@Html.HiddenFor(model => item.Id)
@Html.HiddenFor(model => item.QuestionTx)
<br/>
<br />
foreach (var Question in item.ClassTestQuestionMc)
{
@Html.DisplayFor(model => Question.AnswerTx)
@Html.HiddenFor(model => Question.AnswerTx)
@Html.CheckBoxFor(model => Question.IsChecked)
@Html.HiddenFor(model => Question.IsChecked)
}
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="FinishTest" class="btn btn-default" />
</div>
</div>
在控制器中:
public ActionResult ClassCourseTest(int IdCourse)
{
var classTestQuestions = db.ClassTestQuestions.Include(c=>c.ClassTestQuestionMc).Include(c => c.ClassTest).Where(i=>i.ClassTestId== IdCourse);
return View("ClassCourseTest", classTestQuestions.ToList());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult FinishTest(ClassTestQuestion classTestQuestion)
{
return View(classTestQuestion);
}
我的课堂测试问题模型:
namespace OnlineLearningMVC.Models
{
public class ClassTestQuestion
{
public int Id { set; get; }
public int ClassTestId { set; get; }
public virtual ClassTest ClassTest { set; get; }
[Required]
[DisplayName("Question")]
public string QuestionTx { set; get; }
[Required]
[DisplayName("Order")]
public int OrderInt { get; set; }
[DisplayName("Disabled")]
public bool IsDeleted { set; get; }
public string CreatedFrom { set; get; }
public DateTime CreatedDate { set; get; }
public string UpdatedFrom { set; get; }
public DateTime UpdatedDate { set; get; }
public virtual ICollection<ClassTestQuestionMc> ClassTestQuestionMc { set; get; }
}
我的 ClassTestQuestionMc 模型:
namespace OnlineLearningMVC.Models
{
public class ClassTestQuestionMc
{
public int Id { set; get; }
public int ClassTestQuestionId { set; get; }
public virtual ClassTestQuestion ClassTestQuestion { set; get; }
[Required]
public string AnswerTx { set; get; }
[DisplayName("Is Correct Answer?")]
public bool IsCorrectAnswer { set; get; }
public bool IsChecked { set; get; }
[DisplayName("Disabled")]
public bool IsDeleted { set; get; }
public string CreatedFrom { set; get; }
public DateTime CreatedDate { set; get; }
public string UpdatedFrom { set; get; }
public DateTime UpdatedDate { set; get; }
}
我在浏览器中看到的:
编辑
我尝试更改为 IEnumerable :
将 ClassTestQuestionMc 的 ICollection 更改为 IList。然后你需要在这里遵循数组名称约定,使 MVC 能够处理视图模型。
foreach (int i = 0; i < Model.Count; i++)
{
@Html.DisplayFor(model => Model[i].QuestionTx)
@Html.HiddenFor(model => Model[i].Id)
@Html.HiddenFor(model => Model[i].QuestionTx)
<br/>
<br/>
foreach (int j = 0; j < Model[i].ClassTestQuestionMc; j++)
{
@Html.DisplayFor(model => Model[i].ClassTestQuestionMc[j].AnswerTx)
@Html.HiddenFor(model => Model[i].ClassTestQuestionMc[j].AnswerTx)
@Html.CheckBoxFor(model => Model[i].ClassTestQuestionMc[j].IsChecked)
@Html.HiddenFor(model => Model[i].ClassTestQuestionMc[j].IsChecked)
}
}