problem z usunieciem obrazka .net core

0

Witam ,gdy chce usunac obrazek wyskakuje mi taki błąd :System.ArgumentNullException: 'Value cannot be null.
Parameter name: entity'
MemyController zawiera metode :

 [HttpPost]
        public ActionResult Delete(int idd)
        {
            Memy pom = db.Memy.Find(idd);
            db.Memy.Remove(pom);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

views->memy->index

<style>

    body {
        background-color: grey;
    }

    .foto {
        height: 400px;
        width: 400px;
        margin-left: auto;
        margin-right: auto;
    }

    .table {
        text-align: center;
        font-size: 40px;
        font-family: Arial;
    }

    .opis {
        font-size: 50px;
    }
</style>
<br>


<table class="table">

    @foreach (var item in Model)
    {


        <h1>@item.Title</h1>
        <input type="button" value="Delete" class="btn btn-danger" />

        <a>
            <img class="foto" src="@item.coverImg" />
            <br />
            <br />
        </a>
        <p class="opis">@item.Description</p>
        <td>@item.Description</td>


        <td>@item.Category</td>
        <td>@item.releaseDate</td>
        <td>@item.modifyDate</td>
        <td>@item.Id_mema</td>
        <td>@item.Like</td>
        <td>@item.Dislike</td>

        @if (SignInManager.IsSignedIn(User))
        {
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id_mema }, new { id = item.Id_mema, @class = "btn btn-primary", @style = "margin-bottom: 20px;" })
            </td>
            <td>
                <input type="button" value="Delete" class="btn btn-danger" />
            </td>


            @using (Html.BeginForm("delete", "memy", FormMethod.Post, new { name = "frmMovie", id = "frmMovie" }))
            {
                <input type="hidden" name="id" />
            }
        }



    }
</table>

@section Scripts
    {
    <script>
        $(".btn-danger").click(function () {
            if (!confirm('Do you really want to delete this mem?')) {
                return false;
            }
            var id = $(this).closest("tr").find(".item-id").text();
            document.frmMovie.id.value = id;
            document.getElementById("frmMovie").submit();
        });
    </script>
}

przeglądałem
https://stackoverflow.com/questions/34863167/asp-net-mvc-why-is-my-view-passing-null-models-back-to-my-controller
ale jakoś nie moge zrozumieć co jest źle..
nie rozumiem jaka jest przyczyna tego błędu...

0

No masz null'a. Obstawiam, że przy bindowaniu do idd. Najprościej będzie Ci zrobić jakąś klasę np

public class DeleteImageViewModel
{
 public int Id {get;set;}
}

A akcja w kontrolerze ma przyjmować nie Id tylko właśnie ten viewmodel.
Tylko do inputa hidden wrzuć jeszcze wartość Idka na widoku.

A inny sposób to możesz zostawić akcje w kontrolerze jak jest , tylko zmień idd na id. A na widoku ustawić url do usuwania na /controller/akcja/twójId.

0

Zobacz jak działa twój JavaScript: var id = $(this).closest("tr").find(".item-id").text();. Czyli po kliknięciu przycisku z klasą .btn-danger szuka elementu z klasą .item-id i z niego bierze id, które wkłada do formularza i ten formularz wysyła. A masz gdzieś element, który ma klasę .item-id?

0

masz racje ,nie było takiej klasy,wiec to dodałem,teraz wyglada to tak i nadal błąd

@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Identity
@using MemeGenerator.Models
@model IEnumerable<MemeGenerator.Models.Memy>
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager

@if (SignInManager.IsSignedIn(User))
{
    <p>Jesteś zalogowany</p>
}
else
{
    <p>Nie jesteś zalogowany</p>
}

<h1>@ViewData["Title"]</h1>
@if (SignInManager.IsSignedIn(User))
{


    @Html.ActionLink("+ Dodaj mema", "create", "Memy", null, new { @class = "btn btn-primary", @style = "margin-bottom: 20px;" })

}

<style>

    body {
        background-color: grey;
    }

    .foto {
        height: 400px;
        width: 400px;
        margin-left: auto;
        margin-right: auto;
    }

    .table {
        text-align: center;
        font-size: 40px;
        font-family: Arial;
    }

    .opis {
        font-size: 50px;
    }
</style>
<br>
<table class="table">

    @foreach (var item in Model)
    {
        <h1>@item.Title</h1>





        <input type="button" value="Delete" class="btn btn-danger" />




        <a>
            <img class="foto" src="@item.coverImg" />
            <br />
            <br />
        </a>


        <p class="opis">@item.Description</p>
        <p class="item-id">@item.Id_mema</p>
        <p class="item-id">@item.Autor</p>























}
</table>
@using (Html.BeginForm("Delete", "Memy", FormMethod.Post, new { name = "frmMovie", id = "frmMovie" }))
{
    <input type="hidden" name="id" />
}
@section Scripts
    {
    <script>
        $(".btn-danger").click(function () {
            if (!confirm('Do you really want to delete this mem?')) {
                return false;
            }
            var id = $(this).closest("tr").find(".item-id").text();
            document.frmMovie.id.value = id;
            document.getElementById("frmMovie").submit();
        });
    </script>
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1