2011-09-19 80 views
0

我有一個複雜的對象,我想在編輯視圖中使用。爲了簡化事情,我創建了一個ViewModel併成功創建了編輯視圖頁面,並且所有內容都可以正確渲染。當我點擊保存時,一切都崩潰了。編輯使用ViewModel查看問題

視圖模型如下:

public class ClosureEditViewModel 
{ 

    public Model.Closure Closure { get; set; } 
    public Model.School School { get; set; } 
    public Model.ClosureDetail CurrentDetails { get; set; } 
} 

一些觀的如下:

<div class="display-label">School</div> 
<div class="display-field"> 
    @Html.DisplayFor(model => model.Closure.School.Name) 
</div> 
<div class="display-label">Closed</div> 
<div class="display-field"> 
    @Html.DisplayFor(model => model.Closure.Logged) 
</div> 
.... 
<div class="editor-label"> 
    @Html.LabelFor(model => model.CurrentDetails.DateOpening, "Date Opening (dd/mm/yyyy)") 
</div> 
<div class="editor-field"> 
    @Html.TextBox("DateOpening", Model.CurrentDetails.DateOpening.ToString("dd/MM/yyyy")) 
    @Html.ValidationMessageFor(model => model.CurrentDetails.DateOpening) 
</div> 
.... 
    <tr> 
     <td> 
      @Html.CheckBoxFor(model => model.CurrentDetails.Nursery, (Model.School.Nursery ? null : new { @disabled = "disabled" })) 
     </td> 

控制器的重要組成部分如下:

public ActionResult Edit(int id) 
    { 
     Data.IClosureReasonRepository reasonRepository = new Data.SqlServer.Repositories.ClosureReasonRepository(UnitOfWork); 
     IEnumerable<Model.ClosureReason> reasons = reasonRepository.GetAll(); 

     Model.Closure closure = ClosureRepository.GetClosure(id); 
     Model.ClosureDetail currentDetail = closure.ClosureDetails.Last(); 
     ViewModels.ClosureEditViewModel editClosure = new ViewModels.ClosureEditViewModel() { Closure = closure, School = closure.School, CurrentDetails = closure.ClosureDetails.Last() }; 
     ViewBag.ReasonId = new SelectList(reasons, "Id", "Name", currentDetail.ReasonId); 
     return View(editClosure); 
    } 

    [HttpPost] 
    public ActionResult Edit(ViewModels.ClosureEditViewModel newDetail) 
    { 
     //if (ModelState.IsValid) 
     //{ 

     //} 

     Data.IClosureReasonRepository reasonRepository = new Data.SqlServer.Repositories.ClosureReasonRepository(UnitOfWork); 
     IEnumerable<Model.ClosureReason> reasons = reasonRepository.GetAll(); 
     ViewBag.ReasonId = new SelectList(reasons, "Id", "Name", newDetail.CurrentDetails.ReasonId); 
     return View(newDetail); 
    } 

當我點擊保存,出現如下信息:

Object reference not set to an instance of an object. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 

Source Error: 


Line 94:     </td> 
Line 95:     <td> 
Line 96:      @Html.CheckBoxFor(model => model.CurrentDetails.P1, (Model.School.P1 ? null : new { @disabled = "disabled" })) 
Line 97:     </td> 
Line 98:     <td> 

我只是不知道爲什麼它有問題的學校財產,但其他兩個都沒有。

詹姆斯:-)

回答

1

看來Model.School當你在POST操作再次呈現視圖爲空。請確保它不爲空,因爲在您的視圖中,您沒有綁定到School屬性的單個輸入字段=>此屬性在您的POST控制器操作中將爲空。

[HttpPost] 
public ActionResult Edit(ClosureEditViewModel viewModel) 
{ 
    ... some operations 

    // Make sure that viewModel.School is not null 
    // Remember that the checkbox is bound to CurrentDetails.P1 so 
    // when you post to this action there is nothing that will initialize 
    // the School property => you should do whatever you did in your GET 
    // action in order to initialize this property before returning the view 
    return View(viewModel); 
} 
+0

工作了一段時間,我剛剛從存儲庫中抓取了另一個School對象,並將它添加到ClosureEditViewModel對象School屬性中。 –