2017-04-25 8 views
0

私は、MVC 5、C#と別のモデル(ClassTestQuestionMc)に関連するモデル(ClassTestQuestion)でRazorを使用しています。MVC on submitコントローラにヌルオブジェクトを戻す

Iチェックボックスをオンにして、コントローラで[送信]ボタン(終了)を押すと、nullオブジェクトが戻ってきます。 どのように結果を取り戻すことができますか?ビューで

:コントローラで

@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; } 
    } 

enter image description here私はブラウザでご覧何

enter image description here

編集

私はIEnumerableをに変更しようとしている: enter image description here

+0

あなたのモデルは@model IEnumerable ですが、ポストアクションでClassTestQuestionを期待していますか?それは意味がありません。 – wannadream

+0

あなたは何を提案するのですか? – marios

+0

ビュー内のモデルがIEnumerable の場合、アクションでも 'FinishTest(IEnumerable )'が必要です。 MVCがリストを処理します。 – wannadream

答えて

0

変更いるICollectionのIListにClassTestQuestionMcため。次に、ビューモデルを処理するためにMVCを機能させるために、ここで配列名の規約に従う必要があります。

関連する問題