EB.
EB.

Reputation: 2757

Html Action link not taking me to the correct page

On my Home/index page which is returned by my home controller, I have added an HTML Action Link:

  @Html.ActionLink("Create Ticket", "Create", "CustomerTicketInfo", new { @class = "btn btn-default" })

When clicked it should take me to a Create page. This Create page is returned by my CustomerTicketInfo controller which allows the user to create a ticket and then submit the ticket.

But when I click the action link, I get a server error/ resource not found. It's trying to go to: http://localhost:61517/Home/Create?Length=18, where a Create method does not exist. It exists in the CustomerTicketInfo Controller.

I'm not sure why this fails. I am stating the Action: Create. And the controller: CustomerTicketInfo in the Action link parameters. But yet it still wants to go to the Home Controller? What am I doing wrong?

I could just you an tag, however, I want to do this the MVC way.

Upvotes: 2

Views: 1544

Answers (2)

Dennis R
Dennis R

Reputation: 3237

Yes, Tim's solution would solve your problem. Since it's sometimes hard to remember the correct overloads I prefer constructing the @Html.ActionLink like below so I know what parameter I'm assigning value to.

@Html.ActionLink( linkText: "Create Ticket",
                  actionName: "Create",
                  controllerName: "CustomerTicketInfo",
                  routeValues: null,
                  htmlAttributes: new
                  {
                     @class = "btn btn-default"
                  })

In that way it's quite readable on what's what. If we need to pass in multiple values for the routeValues then it can be done like

routeValues: new
             {
                 type = 'whatever type',
                 id = 'id value'
             },

Upvotes: 1

Tim M.
Tim M.

Reputation: 54359

You are using an overload that is yielding undesirable results:

public static MvcHtmlString ActionLink
  (this HtmlHelper htmlHelper, 
  string linkText, 
  string actionName, 
  object routeValues, 
  object htmlAttributes)
  • param 1: link text
  • param 2: action
  • param 3: not controller, but route data (note how the length of the string "CustomerTicketInfo" == 18)
  • param 4: HTML attributes

Instead, you probably want this:

@Html.ActionLink( "Create Ticket", "Create", "CustomerTicketInfo", null, new { @class = "btn btn-default" } )

Note the extra parameter, which causes the correct overload to be used.

Upvotes: 3

Related Questions