简体   繁体   中英

ASP.NET MVC 2 - Object passed to delete method controller is empty

I'm trying to delete an entry from the database. This is my controller method:

   public ActionResult Delete(Models.CommentEntry entry)
    {
        _db.Entries.Remove(entry);
        _db.SaveChanges();
        return RedirectToAction("Index");
    }

And this is my.cshtml:

         @foreach (var entry in ViewBag.Entries)
            {
                <section class="contact">
                    <header>
                        <h3>@entry.Message</h3>
                    </header>
                    <p>
                        Posted by @entry.Name on @entry.DateAdded.ToLongDateString()
                    </p>
                </section>
                <form method="post" action="Delete">
                    <input type="submit" value="Delete" class="btn btn-danger" />
                </form>
            }

网站

This is how it looks right now. As you can see i want whenever the user presses delete, to delete the corresponding comment. I'm using foreach to access them all and was hoping that this way I can pass the comment object to the controller. 倾倒

As you can see the object doesn't get passed down and always has initial values, therefore i'm getting a dump. Where is my mistake?

Thank you.

You must enter the entry parameters between the form tag. for each parameter in the entry model, declare an input tag with name and value. change the form part code as follow:

<form method="post" action="Delete">
<input type="hidden" name="Message" value="@entry.Message" /> 
<input type="hidden" name="Id" value="@entry.Id" />
 .
 .
<input type="submit" class="btn btn-danger" />
</form>

For more information about sending data to the controller, you can read this article: https://www.c-sharpcorner.com/UploadFile/3d39b4/getting-data-from-view-to-controller-in-mvc/

first of all i think you are here from laravel (i figure out it from you action). here is my code and i explain all:

public async Task<ActionResult> Delete(int Id)
{
    var entry = _db.Entries(x=>x.Id == Id);
    if(entry != null)
    {
        _db.Entries.RemoveAsync(entry);
        _db.SaveChangesAsync();
    }
    return RedirectToAction("Index");
}

now you have to fix you'r cshtml file too, for sending Id to this action

@foreach (var entry in ViewBag.Entries)
{
    <section class="contact">
        <header>
            <h3>@entry.Message</h3>
        </header>
        <p>
            Posted by @entry.Name on @entry.DateAdded.ToLongDateString()
        </p>
    </section>
    <form  action="Delete">
        <input type="hidden" name="Id" value="@entry.Id" />
        <input type="submit" value="Delete" class="btn btn-danger" />
    </form>
}

this not wrong question

<section class="contact">
                <header>
                    <h3>@entry.Message</h3>
                </header>
                <p>
                    Posted by @entry.Name on @entry.DateAdded.ToLongDateString()
                </p>
                 <form method="post" action="Delete"><!-- must have name and value input for form tag -->
            </section>
                <input type="submit" value="Delete" class="btn btn-danger" />
            </form>

Please read this https://www.w3schools.com/tags/tag_form.asp

   // you can use entryId
    
     public ActionResult Delete(int entryId)
        {
             var entry = _db.Entries(x=>x.Id == entryId);
            _db.Entries.Remove(entry);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }
//Get the Id From SubmitButton in View
    @Html.ActionLink("Delete","ControllerName",new{entryid==entry.Id})

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