我抓住了这个错误并且打了我的脑袋 - 这似乎是一个真实的形式,所以这个错误(以及我无法解决它)让我疯了。
我只是想更新表中的1个字段。正在更新的字段可能为空,也可能不为空。
我在控制器中出错了。 'ilpCareerGoal'对象返回null并导致错误。
控制器:
[Authorize]
public ActionResult editCareerGoal(int emplID)
{
ilpCareerGoal careerGoal = qService.getCareerGoal(emplID);
return View(careerGoal);
}
[Authorize]
[HttpPost]
public ActionResult editCareerGoal(ilpCareerGoal careerGoal)
{
try
{
qService.editCareerGoal(careerGoal);
return RedirectToAction("Index", "Home");
}
catch
{
throw;
}
}
查看:
@model ILP.Models.ilpCareerGoal
@{
ViewBag.Title = "editCareerGoal";
}
<h2>Edit Your Career Goal</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm("editCareerGoal", "Home", FormMethod.Post, new { id = "careerGoalForm" }))
{
@Html.ValidationSummary(false)
<fieldset>
<legend>Career Goal</legend>
<div class="editor-label">
@Html.HiddenFor(model => model.emplID)
Record information here about what you're striving for professionally.
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.careerGoal, new { cols = "90", rows = "15" })
</div>
<p>
<input type="submit" value="Update" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
当我调试时,我看到我的控制器中的'careerGoal'为空...我感谢任何帮助!
更新 - 也许它是一个模范的东西?我实际上在不同的模型中有这个字段...这是我的模型代码,以防万一......
型号:
public bool editCareerGoal(ilpCareerGoal tcareerGoal)
{
employeeDataClassesDataContext careerGoals = new employeeDataClassesDataContext();
ilpCareerGoal careerGoal;
try
{
careerGoal = careerGoals.ilpCareerGoals.Single(c => c.emplID == tcareerGoal.emplID);
careerGoal.careerGoal = tcareerGoal.careerGoal;
careerGoals.SubmitChanges();
return true;
}
catch
{
return false;
}
}
答案 0 :(得分:0)
careerGoal
类中的属性ilpCareerGoal
与careerGoal
操作中ilpCareerGoal
类型的editCareerGoal
变量之间的名称冲突可能是导致DefaultModelBinder出现神经衰弱。
尝试将操作中变量的名称更改为不同的名称,例如careerGoalModel
。