0

I have some problems with deletion item from database (SQLServer) using parameters for that. I want to press "Delete" reference in Index() then put name parameter in Delete() and redirect action to Index() again and show content of db. When I press "Delete" reference I show nothing but start page of Index() :(

    public async Task<IActionResult> Delete(string nm)
        {
            IQueryable<Phone> users = db.Phones;

            if (!String.IsNullOrEmpty(nm))
            {
                users = users.Where(p => p.Name.Contains(nm));

                 foreach (var item in users)
                 {
                     db.Phones.Remove(item);

                 }

                 await db.SaveChangesAsync();

            }
            return RedirectToAction("Index");
        }


    @model DataApp2.Models.Phone
@{
    ViewBag.Title = "Delete";
 }


    <form method="get">
        <div class="form-inline form-group">
            <label class="control-label">Name: </label>
            @Html.TextBox("nm", Model.Name, htmlAttributes: new { @class = "form-control" })          
            <input type="submit" value="Delete" class="btn btn-default" />
        </div>
    </form>
mason
  • 31,774
  • 10
  • 77
  • 121

1 Answers1

1

Building the input yourself and using a form is a bit overkill/overcomplicated. Instead, you can leverage the .NET MVC framework to send the request to your action by replacing the form you posted and everything inside of it with:

@Html.ActionLink("Delete", "Delete", new { nm = Model.Name })

This will generate a link (<a> tag) with the text "Delete" (first param of the ActionLink) and send the Model.Name in a data field called nm to the Delete action in your controller (second param of the ActionLink).

I've put together a proof of concept showing that this works:

View:

@Html.ActionLink("Delete", "Delete", new { nm = "hi" })

Controller Action:

    public ActionResult Delete(string nm)
    {
        if (!String.IsNullOrEmpty(nm))
        {
            ViewBag.Name = nm;
        }

        return RedirectToAction("Index");
    }

the controller is successfully setting ViewBag.Name in this example. Note as far as the issue you're having, it makes no difference that I'm returning a ActionResult here instead of async Task<IActionResult> as you are.

I'm guessing that you're not populating Model.Name in the action that initially loads the page. Please post the code for your get action that loads the view if you'd like more information. You can test this theory by sticking:

@if (string.IsNullOrEmpty(Model.Name))
{
    <h1>Name is empty!</h1>
}
else
{
    <h1>Name is @Model.Name</h1>
}

in your view if you dont want to step through the code via the debugger

GregH
  • 5,125
  • 8
  • 55
  • 109
  • Sorry but Idk where should I put these one in my code and where should I write my name parameter. And is everthing correct in my Delete function in Controller? – Александра Юреня Apr 05 '19 at 14:52
  • that goes inside your view and you can delete your entire `
    ...
    ` and replace it with that. your delete action looks ok however I would change `users.Where(p => p.Name.Contains(nm))` to `users.Where(p => p.Name.Contains(nm)).ToList()`... the way you have it has some real negative performance impacts
    – GregH Apr 05 '19 at 14:54
  • Thank you. I put this only string to my View but still have the same problem (when i go "Delete" i redirect to Index page without any action) and really do not understand where we should write name as parameter – Александра Юреня Apr 05 '19 at 15:06
  • @АлександраЮреня please check my updated answer. also your issue has nothing to do with deletion or sql server as your code that is actually being executed is not even hitting those lines. You can verify this yourself if you step through it with a debugger. the issue is that `nm` is null in your controller action – GregH Apr 05 '19 at 15:31
  • 1
    You should use POST for delete instead of a GET: https://stackoverflow.com/questions/786070/why-should-you-delete-using-an-http-post-or-delete-rather-than-get – mxmissile Apr 05 '19 at 15:44
  • doesnt have to do with her issue but I agree post is better than get to delete data- only reason for this is you can include a anti forgery token to stop CSRF attacks. this topic seems way out of scope for where shes at with mvc knowledge right now and i think time is better served helping with the actual issue/basics. good call out though – GregH Apr 05 '19 at 15:52