G
G
GenTs6662019-08-25 21:46:53
ASP.NET
GenTs666, 2019-08-25 21:46:53

How to pass data from view to method?

There is a Customer controller, it has the DeleteCustomer action method.

public class CustomerController : Controller
{
    [HttpGet]
    public IActionResult Index()
    {
        IEnumerable<CustomerViewModel> customers =
        _customerRepository.GetAllCustomers().Select(s => new
        CustomerViewModel
        {
            CustomerId = s.CustomerId,
            Name = s.Name,
            Adress = s.Adress
        });

        return View("Index", customers);
    }

    [HttpPost]
    public IActionResult DeleteCustomer(int id)
    {
        _customerRepository.Delete(id);
        return LocalRedirect("~/Customer/Index");
    }
}

Here is the link, when clicked, the action method should work, the Id of the user I want to delete should arrive in the method. But he writes an error 405. I made a mistake somewhere, I can’t understand where exactly. I will be grateful for help.
<a asp-action="DeleteCustomer" asp-route-id="@item.CustomerId">Delete</a>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir S, 2019-08-25
@GenTs666

the method waits for a post request, and you make a get request with a link

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question