Home > Net >  I'm stuck on this "InvalidOperationException Error" when trying to delete a record in
I'm stuck on this "InvalidOperationException Error" when trying to delete a record in

Time:01-25

The full error I'm getting:

Error

Part of error

I'm passing my index page as a partial view to my _Layout.cshtml that might be the cause of my error, but I'm not sure what I'm doing wrong and why is it also affecting my MC_Delete page as well? My Index view looks like my Manage Customer View but you can only delete.

_Layout.cshtml
(I know I made a typo when I created the project)

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - Custstomer_Management_System</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
    <header>
        <nav >
            <div >
                @*<a  asp-area="" asp-controller="Home" asp-action=""></a>*@
                <button  type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span ></span>
                </button>
                <div >
                    <ul >
                        <li >
                             <a  asp-area="" asp-controller="Customer" asp-action="Index">Home</a>
                          </li>
                        <li >
                            <a  asp-area="" asp-controller="Customer" asp-action="ManageCustomer">Manage Customer</a>
                        </li>
                        @*<li >
            <a  asp-area="" asp-controller="Customer" asp-action="Delete">Create</a>
        </li>*@
                    </ul>
                </div>
            </div>
            @*<a href="C:\Users\winte\Desktop\Arksoft\Custstomer Management System\Views\Home\Privacy.cshtml">C:\Users\winte\Desktop\Arksoft\Custstomer Management System\Views\Home\Privacy.cshtml</a>*@
        </nav>
    </header>
    <div >
        <main role="main" >
            @RenderBody()
            @{
                Html.Partial("Index");
             }
        </main>
    </div>

    <footer >
        <div >
            &copy; 2022 - Custstomer_Management_System - <a asp-area="" asp-controller="Home" asp-action="Privacy"></a>
        </div>
    </footer>
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>
    @await RenderSectionAsync("Scripts", required: false)
</body>
</html>   

Manage Customer view

@model IEnumerable<Custstomer_Management_System.Models.Customer>

@{
    ViewData["Title"] = "ManageCustomer";
}

<h1>Manage Customer</h1>

<p>
    <a  asp-area="" asp-controller="Customer" asp-action="Create">Create New Customer</a>
</p>
<table >
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Address)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.TelNo)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ContactPersonName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ContactPersonEmail)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.VatNo)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TelNo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ContactPersonName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ContactPersonEmail)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.VatNo)
            </td>
            <td>
                <a asp-action="Update" asp-route-id="@item.Id" >Edit</a> 
                @*<a asp-action="Details" asp-route-id="@item.Id">Details</a> |*@
                <a asp-action="MC_Delete" asp-route-id="@item.Id" >Delete</a>
            </td>
        </tr>
}
    </tbody>
</table>

Delete view

@model Custstomer_Management_System.Models.Customer

@{
    ViewData["Title"] = "MC_Delete";
}

<h1>MC_Delete</h1>

<h3>Are you sure you want to delete this?</h3>
<div>
    <h4>Customer</h4>
    <hr />
    <dl >
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.Name)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.Name)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.Address)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.Address)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.TelNo)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.TelNo)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.ContactPersonName)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.ContactPersonName)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.ContactPersonEmail)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.ContactPersonEmail)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.VatNo)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.VatNo)
        </dd>
    </dl>
    
    <form asp-action="MC_DeletePost">
        <input type="hidden" asp-for="Id" />
        <input type="submit" value="Delete"  /> |
        <a asp-action="Index">Back to List</a>
    </form>
</div>

Controller

    //GET
        public IActionResult MC_Delete(int? id)
        {
            if (id == null || id == 0)
            {
                return NotFound();
            }

            var obj = _db.Customer.Find(id);
            if (obj == null)
            {
                return NotFound();
            }


            return View(obj);
        }

        //POST Delete
        [HttpPost]
        public IActionResult MC_DeletePost(int? id)
        {
            var obj = _db.Customer.Find(id);
            if (obj == null)
            {
                return NotFound();
            }

            _db.Customer.Remove(obj);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

CodePudding user response:

It seems that the Layout the View is using expects a model of "Customer[]" (list of customers) and you are passing it a Customer. It seems somewhere in your code you should use

return View(_db.Customer);

instead of

return View(_db.Customer.Find(id));

CodePudding user response:

In your Manage Customer view, you are expecting an IEnumerable type i.e. List of Customers when instead you are passing a single Customer Object from Controller.

As per the code in your view, it should work fine if you remove the IEnumerable from the @model declaration at top as you are accessing properties of Customer object which can be accessed like this in a list.

  •  Tags:  
  • Related