1

Hello People i have a list of Customers (with Name and Id) that i have hardcoded in my Customers Controller as: Click to view Customer Controller i am sending list to a view named Customer to display all the three customers with their names as links using following code :

@model IEnumerable<App.Models.Customer>
@{
    ViewBag.Title = "Customer";
}

@foreach (var customer in Model)
{
    <li>
        @Html.ActionLink(customer.Name, "Details", "Customers", new { id = customer.Id }, null)
    </li>
}

It displays list of customers but problem is when i click any customer name(Which is a link) it gives a HTTP 404 error as :Click to see Error details i have made a Details method in Costumers Controller as

[HttpPost]
public ActionResult Details(int id)
{
    var customer = GetCustomers().SingleOrDefault(x => x.Id == id);
    if (customer == null)
    {
        return HttpNotFound();
    }
    return View(customer);
}

that recieves id of the customer name being clicked and simply displays its name again but it doesnt works and gives 404 error i have debugged the code and what i have found that control never reached the details Method in Controller, is the issue in ActionLink or what ? Help is highly appreciated thank you

2
  • You cannot navigate to a POST method. Remove the [HttpPost] attribute from the Details() method Commented Jan 27, 2018 at 10:37
  • yes i have understood it sir thanks you Commented Jan 27, 2018 at 10:52

1 Answer 1

1

The method should be HttpGet:

[HttpGet]
        public ActionResult Details(int id)
        {
            var customer = GetCustomers().SingleOrDefault(x => x.Id == id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }
Sign up to request clarification or add additional context in comments.

1 Comment

thank you sir it worked, i have been stuck here and this was the issue lol. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.