我的删除视图如下所示:
@model Pdsl.Sms.Entities.Models.Common.BranchInfo
@{
ViewBag.Title = "Delete";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>BranchInfo</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.CompanyId)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.CompanyId)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.BranchTypeId)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.BranchTypeId)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.BranchName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.BranchName)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.Description)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Description)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.EntryDate)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.EntryDate)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.EntryBy)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.EntryBy)
</div>
<div>
@Html.Partial("Delete_ContactDetailPartialView", Model.ContactDetails)
//partial view
</div>
</fieldset>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "Index")
</p>
}
我在控制器中的操作如下所示:
private long cDetailId;
public ActionResult Delete(int id)
{
BranchInfo branchInfo = branchInfoService.getABrachInfo(id);
if (branchInfo == null)
{
return HttpNotFound();
}
cDetailId = (long)branchInfo.ContactDetailId;
branchInfo.ContactDetails = contactDetailService.getAContactDetails(cDetailId);
return View(branchInfo);
}
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
try
{
branchInfoService.deleteBranchInfo(id);
contactDetailService.deleteContactDetail(cDetailId);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
删除确认显示正确。但是当点击删除按钮时出现以下错误:
Object reference not set to an instance of an object.
Line 55:
Line 56: <div>
Line 57: @Html.Partial("Delete_ContactDetailPartialView", Model.ContactDetails)
Line 58: </div>
Line 59: