我被這個錯誤所困擾,並且打我的腦袋 - 它好像是一個真正的形式,所以這個錯誤(以及我無法解決它)讓我瘋了。未將對象引用設置爲對象的實例 - 表單對象爲空?
我只是試圖更新表中的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