简体   繁体   中英

I'm stuck on this "InvalidOperationException Error" when trying to delete a record in my Database

The full error I'm getting:

错误

部分错误

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 class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                @*<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action=""></a>*@
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                             <a class="nav-link text-dark" asp-area="" asp-controller="Customer" asp-action="Index">Home</a>
                          </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Customer" asp-action="ManageCustomer">Manage Customer</a>
                        </li>
                        @*<li class="nav-item">
            <a class="nav-link text-dark" 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 class="container">
        <main role="main" class="pb-3">
            @RenderBody()
            @{
                Html.Partial("Index");
             }
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &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 class="btn btn-primary" asp-area="" asp-controller="Customer" asp-action="Create">Create New Customer</a>
</p>
<table class="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" class="btn btn-primary mx-1">Edit</a> 
                @*<a asp-action="Details" asp-route-id="@item.Id">Details</a> |*@
                <a asp-action="MC_Delete" asp-route-id="@item.Id" class="btn btn-danger mx-1">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 class="row">
        <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" class="btn btn-danger" /> |
        <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");
        }

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

In your Manage Customer view, you are expecting an IEnumerable type ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM