0
我使用MVC 5,C#和與另一個模型(ClassTestQuestionMc)相關的模型(ClassTestQuestion)的剃刀。MVC上提交空對象回控制器
我檢查一些複選框,當我按控制器中的提交按鈕(完成)時,我得到空對象。 我怎樣才能找回結果?
在View:
@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);
}
我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; }
}
編輯
您的模型在視圖中是@model IEnumerable但您期望在後操作中使用ClassTestQuestion?它沒有任何意義。 –
wannadream
那麼你提出什麼解決方案? – marios
如果模型在視圖中是IEnumerable,那麼動作也應該期待它,'FinishTest(IEnumerable )'。 MVC將爲您處理該列表。 –
wannadream