2011-09-19 12 views
0

私は編集ビューで使用したい複雑なオブジェクトを持っています。私はViewModelを作成し、編集ビューページを作成してすべてを正しくレンダリングしています。私がセーブすると、すべてが崩れ落ちます。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> 

私は学校のプロパティに問題があるのはなぜですか、他の2つの問題はないのか分かりません。

答えて

1

:-)ジェームズあなたがPOSTアクションに再びビューをレンダリングするときModel.Schoolがnullであると思われます。あなたのビューでは、Schoolプロパティにバインドされた単一の入力フィールドがないので、nullでないことを確認してください。=>このプロパティは、POSTコントローラアクション内ではnullになります。

[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プロパティに追加しました。 –

関連する問題