deep
deep

Reputation: 23

Redirect to a controller's action from another controller while action name is same in both controllers

I have two controller Controller1 and Controller2.
I am redirecting to a action name Action1 of controller Controller1 from the controller Controller2 while in controller Controller2 there is a action names Action1.
So when I am using

@Html.ActionLink("Cancel", "Action1", "Controller1", new { id = "btnCancel", @class = "btn btn-lg btn-primary btn-block FLarge " }) 

It redirects to the the action Action1 of Controller2 .

Please can you help me out on this

Upvotes: 2

Views: 62

Answers (1)

Jonesopolis
Jonesopolis

Reputation: 25370

Ah, posting that actual code you were using helps. You are actually using this overload:

public static MvcHtmlString ActionLink(
   this HtmlHelper htmlHelper,
   string linkText,
   string actionName,
   Object routeValues,
   Object htmlAttributes
)

you want this one:

public static MvcHtmlString ActionLink(
   this HtmlHelper htmlHelper,
   string linkText,
   string actionName,
   string controllerName,
   Object routeValues,
   Object htmlAttributes
)

Essentially, add an empty object before your htmlAttributes. You'll notice from the documentation there is no overload for string, string, string, object. You have to be careful with those @Html overloads, it's very easy to grab the wrong one without a compiler error.

In the end, you want:

@Html.ActionLink("Cancel", "Action1", "Controller1", null, new { id = "btnCancel", @class = "btn btn-lg btn-primary btn-block FLarge " })

Upvotes: 4

Related Questions