简体   繁体   中英

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

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 " })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM