2014-08-27 27 views
0

StudentModel。複選框列表 - 在發佈:創建 - NullReferenceException

namespace mvcApp.Models 
{ 
    public class StudentModel 
    { 
     [Display(Name = "First Name")] 
     public string FirstName { get; set; } 
     [Display(Name = "Last Name")] 
     public string LastName { get; set; } 
     [Display(Name = "Email Address")] 
     public string EmailAddress { get; set; } 

     public List<SchoolOrganization> Organizations { get; set; } 
    } 

    public class SchoolOrganization 
    { 
     public string Name { get; set; } 
     public bool IsInvolved { get; set; } 
    } 
} 

學生涉及多個組織。

控制器

namespace mvcApp.Controllers 
{ 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application."; 

      return View(); 
     } 


     public ActionResult StudentInformation() 
     { 
      // populate student with data 
      var student = new StudentModel() { FirstName = "Joe", LastName = "Doe", EmailAddress = "[email protected]"}; 

      // Populate with Organizations 
      student.Organizations = new List<SchoolOrganization>(); 

      student.Organizations.Add(new SchoolOrganization() { Name = "Math Club", IsInvolved = true}); 
      student.Organizations.Add(new SchoolOrganization() { Name = "Chess Club", IsInvolved = false }); 
      student.Organizations.Add(new SchoolOrganization() { Name = "Football", IsInvolved = true }); 

      return View(student); 
     } 

     **[HttpPost] 
     public ActionResult StudentInformation(StudentModel student) 
     { 
      Response.Write("Name: " + student.FirstName); 

      foreach (var o in student.Organizations) 
      { 
       Response.Write(o.Name + " : " + o.IsInvolved.ToString()); 
      } 
      return View(); 
     }** 
    } 
} 

數據將從數據庫中最終填充。

查看

@model mvcApp.Models.StudentModel 

@{ 
    ViewBag.Title = "StudentInformation"; 
} 

<h2>StudentInformation</h2> 

@using (Html.BeginForm()) { 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>StudentModel</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.FirstName) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.FirstName) 
      @Html.ValidationMessageFor(model => model.FirstName) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.LastName) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.LastName) 
      @Html.ValidationMessageFor(model => model.LastName) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.EmailAddress) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.EmailAddress) 
      @Html.ValidationMessageFor(model => model.EmailAddress) 
     </div> 

     <div> 
      <table> 
        <tr> 

         <td>Organization Name</td><td>Is Involved</td> 
        </tr> 
       @for (int i = 0; i < Model.Organizations.Count; i++) <== System.NullReferenceException here 
       { 
        @Html.HiddenFor(m => m.Organizations[i].IsInvolved) 
        <tr> 
         <td>@Html.DisplayFor(m => m.Organizations[i].Name)</td> 
         <td>@Html.CheckBoxFor(m => m.Organizations[i].IsInvolved)</td> 
        </tr> 
       } 
      </table> 

     </div> 
     <p> 
      <input type="submit" value="Save" /> 
     </p> 
    </fieldset> 
} 

上述代碼顯示細跟HttGet。但是,當我嘗試更新我得到System.NullReferenceException。 https://www.dropbox.com/s/dz0bg3hkd0yq8e3/studentInformation.png?dl=0

任何人都可以請幫忙看看是怎麼回事?

謝謝。

回答

0

在您提供的代碼示例中; HomeController的[HttpPost] ActionResult for StudentInformation不會創建更新的對象模型的新實例並將其傳遞給視圖,而是運行基本的debug.writeline例程。因此,[StudentInformation.cshtml]的[HttpPost]視圖的相關視圖不會收到已更新模型的已填充實例...

在模型的第一行上爲「StudentInformation .cshtml「頁面並運行該應用程序將演示該頁面頂部引用的模型,其中沒有數據...

這就是爲什麼頁面的[HttpPost]版本只是呈現空白模型,沒有任何你可能已經創建的數據值的改變,直到它到達視圖依賴於在頁面第一行調用的模型中必須存在的新數據值的計數的部分...繼續。

一個空的數據模型集導致空引用,因爲沒有要在其中計數的值。

爲了查看更新後的一組設置,視圖模型的[HttpPost]版本必須通過返回新信息的模型實例,如「return View(nameOfViewDataSet)」(您構建數據再次設置,並將其作爲模型的新版本傳遞,修改後的表單數據存在)。

直到通過返回的View語句相對於StudentInformation ActionResult的[HttpPost]版本傳遞的數據到實際視圖,將不會有顯示數據,並且count函數將繼續返回null值。

我希望這會有幫助。