1

我的删除视图如下所示:

@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:
4

1 回答 1

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。

于 2013-09-06T22:05:54.253 回答