Post Action中的ViewModel属性为空

时间:2014-04-03 14:17:08

标签: c# asp.net asp.net-mvc asp.net-mvc-5

现在已修复。下面结合Ish的建​​议加上在视图中添加对@HiddenFor的调用解决了这个问题。

我有一个ASP.NET MVC 5 Web应用程序,用户可以将缺陷标记为已解决。我想显示一个可能相关的缺陷列表,用户可以勾选以表明是,这是相同的缺陷,并且还应标记为已解决。

所以我有一个View Model,它的属性是一个集合,每个成员都包含一个缺陷对象属性和Boolean IsSameDefect属性。这一切在GET动作方法和视图中都可以正常工作。我可以显示相关的缺陷并打勾。

当我想更新数据时,POST操作出现问题。此时,属性(可能相关缺陷的集合)为空。我很难弄清楚如何将这些数据传回控制器?

按要求编码......

// GET: /DefectResolution/Create
public ActionResult Create(int ciid)
{
    int companyId = User.CompanyID();
    DefectResolutionCreateViewModel drcvm = new DefectResolutionCreateViewModel(ciid, companyId);
    return View(drcvm);
}

// POST: /DefectResolution/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(DefectResolutionCreateViewModel drcvm)
{
    DefectResolutions currentResolution = drcvm.DefectResolution;
    currentResolution.CreatedOn = System.DateTime.Now;
    currentResolution.UserID = User.UserID();

    if (ModelState.IsValid)
    {
        unitOfWork.DefectResolutionRepository.Insert(currentResolution);

        if (currentResolution.ResolutionStatusID == 2)
        {
            //code breaks here as drcvm.RelatedUnresolvedDefects is null
            foreach (var relatedDefect in drcvm.RelatedUnresolvedDefects)
            {
                if (relatedDefect.IsSameDefect)
                {
                    DefectResolutions relatedResolution = new DefectResolutions();
                    relatedResolution.ChecklistID = relatedDefect.RelatedChecklist.ChecklistID;
                    relatedResolution.CreatedOn = System.DateTime.Now;
                    relatedResolution.ResolutionNote = currentResolution.ResolutionNote;
                    relatedResolution.ResolutionStatusID = currentResolution.ResolutionStatusID;
                    relatedResolution.UserID = User.UserID();
                }
            }
        }

        unitOfWork.Save();
        return RedirectToAction("Index", new { ciid = currentResolution.ChecklistID });
    }
    return View(drcvm);
}

在视图中......

@model Blah.ViewModels.DefectResolution.DefectResolutionCreateViewModel
@{
    ViewBag.Title = "Create Defect Resolution";
    var relatedDefects = Model.RelatedUnresolvedDefects;
}

......后来在视图中......

 @for (int i = 0; i < relatedDefects.Count(); i++ )
{
    <tr>
        <td>
            @Html.EditorFor(x => relatedDefects[i].IsSameDefect)
        </td>
    </tr>
}                       

我按照下面的Ish建议,修改了代码以直接引用Model.RelatedUnresolvedDefects,而不是像我一直使用的那样使用变量。这确实让我更进一步。视图模型的RelatedUnresolvedDefects属性不再为null。但只有RelatedUnresolvedDefects.IsSameDefect有一个值。 RelatedUnresolvedDefects.RelatedChecklist为null。这是控制器代码,再次显示它现在断开的地方......

// POST: /DefectResolution/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(DefectResolutionCreateViewModel drcvm)
{
    DefectResolutions currentResolution = drcvm.DefectResolution;
    currentResolution.CreatedOn = System.DateTime.Now;
    currentResolution.UserID = User.UserID();

    if (ModelState.IsValid)
    {
        unitOfWork.DefectResolutionRepository.Insert(currentResolution);

        if (currentResolution.ResolutionStatusID == 2)
        {
            //prior to change, code used to break here
            foreach (var relatedDefect in drcvm.RelatedUnresolvedDefects)
            {
                if (relatedDefect.IsSameDefect)
                {
                    DefectResolutions relatedResolution = new DefectResolutions();

                    //code now breaks here because relatedDefect.RelatedChecklist is null
                    relatedResolution.ChecklistID = relatedDefect.RelatedChecklist.ChecklistID;
                    relatedResolution.CreatedOn = System.DateTime.Now;
                    relatedResolution.ResolutionNote = currentResolution.ResolutionNote;
                    relatedResolution.ResolutionStatusID = currentResolution.ResolutionStatusID;
                    relatedResolution.UserID = User.UserID();
                }
            }
        }

        unitOfWork.Save();
        return RedirectToAction("Index", new { ciid = currentResolution.ChecklistID });
    }
    return View(drcvm);
}

1 个答案:

答案 0 :(得分:1)

不知道你的代码。我建议你在View(.cshtml)中渲染缺陷时使用for循环代替foreach

根据您的代码编辑答案 以下语句在视图中创建问题

 var relatedDefects = Model.RelatedUnresolvedDefects;

您应该直接遍历循环中的Model.RelatedUnresolvedDefects属性。

             @for (int i = 0; i < Model.RelatedUnresolvedDefects.Count(); i++ )
                        {
                            <tr>
                                <td>
                                    @Html.EditorFor(x => Model.RelatedUnresolvedDefects[i].IsSameDefect)
                                </td>
                            </tr>
                        }
相关问题