我的删除视图如下所示:
@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:
答案 0 :(得分:1)
使用脚手架时,默认情况下它不会映射子实体。在您的视图中,您需要添加部分视图,还需要编写代码以显示并正确保存它们。
我建议首先在BranchInfo类中添加一个默认构造函数:
public class BranchInfo
{
public BranchInfo()
{
BranchName = "";
Description = "";
EntryDate = DateTime.Now;
EntryBy = "";
ContactDetails = new ContactDetails();
}
[Key]
public int CompanyId { get; set; }
public int BranchTypeId { get; set; }
public string BranchName { get; set; }
public string Description { get; set; }
public DateTime EntryDate { get; set; }
public string EntryBy { get; set; }
public ContactDetails ContactDetails {get;set;}
}
当您创建新的BranchInfo时,请在控制器中传递该类的实例:
// GET: /BranchInfo/Create
public ActionResult Create()
{
BranchInfo model = new BranchInfo();
return View(model);
}
您的Create.cshtml可以附加部分视图:
<div class="editor-label">
@Html.LabelFor(model => model.ContactDetails)
</div>
<div class="editor-field">
@Html.Partial("Create_ContactDetailPartialView", Model.ContactDetails)
</div>
现在每次保存任何BranchInfo时,它都有一个子ContactDetails。因此,当您尝试删除时,可以毫无困难地传递ContactDetails。