0

I'm trying to call a controller method using an ActionLink. I can access the controller method, and even use css to make the ActionLink look like the button it's supposed to, but I can't pass in parameters - every time I try, I either get an error or the values turn up as null.

These will actually reach the method and hit the break point I set, but without any parameter values.

<p>@Html.ActionLink("Submit Request", "Create", "HomeController",  new { @class = "btn btn-primary"})</p>

<p>@Html.ActionLink("Submit Request", "Create", "HomeController",  new { @class = "btn btn-primary", title = "hello", description = "world"})</p>

These give me an error as soon as I click on the button, specifically an HTTP 404 error for /HomeController/Create.

<p>@Html.ActionLink("Submit Request", "Create", "HomeController", new { title = "hello", description = "world" }, new { @class = "btn btn-primary"})</p>

<p>@Html.ActionLink("Submit Request", "Create", "HomeController", new { @class = "btn btn-primary"}, new { title = "hello", description = "world" })</p>

Here's the method in HomeController for the sake of completeness"

    public ActionResult Create(string title, string description)
    {
        try
        {
            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            return View("Index2");
        }
    }

Can anyone tell me what I'm missing or doing wrong?

2
  • If your controller is named "HomeController" then the ActionLink helper will use the string "Home" for the controller parameter. Commented Apr 23, 2019 at 17:58
  • @Jasen oh my god thank you that did the trick at long last. Commented Apr 23, 2019 at 18:14

1 Answer 1

1

Your first attempt is using this version of the overload

@Html.ActionLink(linkText: "Submit Request",
                 actionName: "Create",
                 routeValues: "HomeController",
                 htmlAttributes: new { @class = "btn btn-primary" })

The second version of your attempt cannot find the "/HomeController/Create" so unless your controller is HomeControllerController you'll need to drop "Controller" from the controllerName.

@Html.ActionLink(linkText: "Submit Request",
                 actionName: "Create",
                 controllerName: "Home",
                 routeValues: new { title = "hello", description = "world" },
                 htmlAttributes: new { @class = "btn btn-primary" })
Sign up to request clarification or add additional context in comments.

Comments

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.