使用ViewModel编辑视图的问题

时间:2011-09-19 15:50:16

标签: asp.net-mvc-3

我想在编辑视图中使用一个复杂的对象。为简化起见,我创建了一个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 个答案:

答案 0 :(得分:1)

在POST操作中再次呈现视图时,似乎Model.School为空。确保它不是null,因为在您的视图中,您没有绑定到School property =&gt;的单个输入字段。在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);
}