【发布时间】:2014-04-03 14:17:08
【问题描述】:
现在已修复。下面 Ish 的建议加上在视图中添加对 @HiddenFor 的调用解决了这个问题。
我有一个 ASP.NET MVC 5 Web 应用程序,用户可以在其中将缺陷标记为已解决。我想显示一个潜在相关缺陷的列表,用户可以勾选复选框以表明是的,这是同一个缺陷,也应该标记为已解决。
所以我有一个视图模型,它的属性是一个集合,它的每个成员都包含一个缺陷对象属性和布尔值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 属性不再为空。但只有 RelatedUnresolvedDefects.IsSameDefect 有值。 RelatedUnresolvedDefects.RelatedChecklist 为空。这是控制器代码,再次显示了它现在中断的位置...
// 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);
}
【问题讨论】:
-
请贴一些代码。只有这样我们才能帮助您解决问题所在。
标签: c# asp.net asp.net-mvc asp.net-mvc-5