Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 使用ViewModel编辑视图时出现问题_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 使用ViewModel编辑视图时出现问题

Asp.net mvc 3 使用ViewModel编辑视图时出现问题,asp.net-mvc-3,Asp.net Mvc 3,我想在编辑视图中使用一个复杂的对象。为了简化工作,我创建了一个ViewModel,并成功创建了“编辑视图”页面,所有内容都正确呈现。当我点击save时,一切都崩溃了 ViewModel如下所示: public class ClosureEditViewModel { public Model.Closure Closure { get; set; } public Model.School School { get; set; } public Model.Closur

我想在编辑视图中使用一个复杂的对象。为了简化工作,我创建了一个ViewModel,并成功创建了“编辑视图”页面,所有内容都正确呈现。当我点击save时,一切都崩溃了

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);
    }
部分意见如下:

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);
    }
学校
@DisplayFor(model=>model.Closure.School.Name)
关闭
@DisplayFor(model=>model.Closure.Logged)
....
@Html.LabelFor(model=>model.CurrentDetails.DateOpening,“日期开始(dd/mm/yyyy)”)
@Html.TextBox(“DateOpening”,Model.CurrentDetails.DateOpening.ToString(“dd/MM/yyyy”))
@Html.ValidationMessageFor(model=>model.CurrentDetails.DateOpen)
....
@CheckBoxFor(model=>model.CurrentDetails.Nursery,(model.School.Nursery?null:new{@disabled=“disabled”}))
控制器的重要部件如下:

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);
    }
public ActionResult编辑(int-id)
{
Data.IClosureReasonRepository-reasonRepository=new Data.SqlServer.Repositories.ClosureReasonRepository(UnitOfWork);
IEnumerable 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=新的选择列表(原因,“Id”,“名称”,currentDetail.ReasonId);
返回视图(editClose);
}
[HttpPost]
公共操作结果编辑(ViewModels.ClosureEditViewModel newDetail)
{
//if(ModelState.IsValid)
//{
//}
Data.IClosureReasonRepository-reasonRepository=new Data.SqlServer.Repositories.ClosureReasonRepository(UnitOfWork);
IEnumerable reasons=reasonRepository.GetAll();
ViewBag.ReasonId=新选择列表(原因,“Id”,“名称”,newDetail.CurrentDetails.ReasonId);
返回视图(newDetail);
}
当我点击save时,出现以下消息:

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>
对象引用未设置为对象的实例。
描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
源错误:
第94行:
第95行:
第96行:@Html.CheckBoxFor(model=>model.CurrentDetails.P1,(model.School.P1?null:new{@disabled=“disabled”}))
第97行:
第98行:
我只是不明白为什么学校的财产有问题,但其他两个都没有


James:-)

在后期操作中再次渲染视图时,
Model.School
似乎为空。请确保它不为null,因为在您的视图中,您没有将单个输入字段绑定到
School
属性=>此属性在POST controller操作中为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);
}

我从存储库中抓取了另一个学校对象,并将其添加到ClosureEditViewModel对象学校属性中。