Radoslav Pavlov
Radoslav Pavlov

Reputation: 29

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

Website

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. Dump

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.

Upvotes: 1

Views: 740

Answers (4)

mohammad mobasher
mohammad mobasher

Reputation: 777

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

Upvotes: 1

Sagar Shanks
Sagar Shanks

Reputation: 1

   // 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})

Upvotes: 0

Farshad Delavarpour
Farshad Delavarpour

Reputation: 205

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/

Upvotes: 1

Alireza heidari
Alireza heidari

Reputation: 45

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

Upvotes: 0

Related Questions