我被这个错误所困扰,并且打我的脑袋 - 它好像是一个真正的形式,所以这个错误(以及我无法解决它)让我疯了。未将对象引用设置为对象的实例 - 表单对象为空?
我只是试图更新表中的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;
}
}
看着代码一切似乎都对我好。你能否发布初始化视图的代码? – Iridio