Search code examples
asp.netasp.net-mvc-4viewpartial

Delete partial view info with parent view in asp.net MVC4


My Delete view is like the following :

@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>
}

My actions in the controller look like the following:

    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();
        }
    }

Delete confirmation is showing properly. But when click delete button the following error is appeared:

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:

Solution

  • When you use scaffolding,by default it does not map child entity. In your view you need to add partial view, and you also need to write code to display and save them properly.

    First thing I would suggest to add a default constructor in the BranchInfo class:

    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;} 
    }
    

    And when you create a new BranchInfo, pass an instance of the class in your controller:

    // GET: /BranchInfo/Create
    
    public ActionResult Create()
    {
        BranchInfo model = new BranchInfo();
        return View(model);
    }
    

    And your Create.cshtml you can attach a partial view:

    <div class="editor-label">
        @Html.LabelFor(model => model.ContactDetails)
    </div>
    <div class="editor-field">
        @Html.Partial("Create_ContactDetailPartialView", Model.ContactDetails) 
    </div>
    

    Now each time you save any BranchInfo, it has a child ContactDetails. So, when you try to delete, you can pass the ContactDetails without any trouble.