Solved

MVC HTML.Actionlink to route to different controller action

Posted on 2011-03-24
14
1,833 Views
Last Modified: 2013-11-08
What is the best approach to route the following to a controller action from anywhere with an MVC app?  The following code is everywhere in the app and I need to make it a link so that it routes back to a user details page.   I have two areas in the app  SuperAdmin and TeamAdmin and I want to redirect back to the SuperAdmin edit user page if in  SuperAdmin area and route back to the TeamAdmin area if in TeamAdmin.   I guess I may need to replicate a bit of edit user functionality as they both do different things.

So this needs amending so if clicked on it goes to the users controller "details" action passing the userID

 <%=  Html.Encode(Model.OriginatingUser.FirstName + ", " + Model.OriginatingUser.Surname) %>



0
Comment
Question by:ToString1
  • 5
  • 5
  • 4
14 Comments
 
LVL 16

Assisted Solution

by:ToddBeaulieu
ToddBeaulieu earned 187 total points
ID: 35208576
Well you can easily link to a named controller simply with a link to it.

<a href="/Home/Index">Home Controller</a>

I've got links like this in a development jump off page, so I can redirect to one of several controllers.

And then you have options for building the URL.

<input type="button" value="Cancel" onclick="window.location.href='@Url.Action("AnotherAction", new { id = @Model.SomeId })#@hash'" class="button" />

That helper also allows you to specify the controller name. Of course, you don't need a helper.
0
 
LVL 27

Assisted Solution

by:nmarun
nmarun earned 313 total points
ID: 35208730
Or you can use the ActionLink method directly:

Html.ActionLink(Model.LastName + ", " + Model.FirstName, "Details", new {id=Model.Id })

This will take you to the details action method of the current controller.

Arun
0
 

Author Comment

by:ToString1
ID: 35208748

I need something this

Html.ActionLink( Html.Encode(Model.User.Username), "RedirectToUser", "UserController",  new {id=Model.User.Id } )%>

So if it is made into a user control it will redirect to the user details page
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 35208780
I'm pobably being thick ... so what is the issue? You can include all those arguments with ActionLink, right?

 actionlink
0
 
LVL 27

Assisted Solution

by:nmarun
nmarun earned 313 total points
ID: 35208844
Also, you might not want to add the controller in the ActionLink. This way you can just copy and paste this code and not have to worry about the area you're in.

Arun
0
 

Author Comment

by:ToString1
ID: 35208848
I'm still getting to grips with MVC.

Could you provide a code example please modifying this?    

Html.ActionLink( Html.Encode(Model.User.Username), "RedirectToUser", "UserController",  new {id=Model.User.Id } )%>
0
 
LVL 27

Assisted Solution

by:nmarun
nmarun earned 313 total points
ID: 35208904
Let's step back and see this.

What happens when you add the below line and run the application:

<%= Html.ActionLink( Html.Encode(Model.User.Username), "RedirectToUser", "UserController",  new {id=Model.User.Id } )%>

Arun
0
 

Author Comment

by:ToString1
ID: 35209231
IT actually redirects to

applicationName/RedirectToUser    

It thinks that "RedirectToUser" is a view   - where in my code ive got a redirectToAction called "RedirectToUser"


Wherever I am in the application I want to route it to the Users conroller "edit" method


0
 
LVL 16

Assisted Solution

by:ToddBeaulieu
ToddBeaulieu earned 187 total points
ID: 35209246
I think it's picking up the wrong constructor. Look at the screenshot I attached. Is that the contructor you're using?

If not, supply the additional argument to make it happy.
0
 

Author Comment

by:ToString1
ID: 35209259
So I guess what I am saying is that I want to implement the following

I have a username   "Doe, Jon"    Clicking on that from wherever it is displayed in any view I want to navigate to    

myApp/Users/Edit/1

As I already know the userID



0
 
LVL 27

Assisted Solution

by:nmarun
nmarun earned 313 total points
ID: 35209273
So you want a link to be displayed on the web page that shows the encoded username as the text and redirects to a link similar to 'users/edit/2'. If this is right then try the below:

<%= Html.ActionLink( Html.Encode(Model.User.Username), "Edit", "Users",  new {id=Model.User.Id } )%>

Arun
0
 
LVL 16

Assisted Solution

by:ToddBeaulieu
ToddBeaulieu earned 187 total points
ID: 35209306
Again, I think you're missing the point guys.

Look at the attached constructor.

There is no constructor to give you what you want without the additional argument that you're missing.

link test
action
controller
route objects
html attributes

Invoke the intellisense dropdown to show you the available arguments and validate that what you're trying to feed the helper is what it's looking for. Bet it's not!

0
 
LVL 27

Accepted Solution

by:
nmarun earned 313 total points
ID: 35209394
@Html.ActionLink(Model.LastName + ", " + Model.FirstName, "Details", "myApp/Users", new {id=Model.Id }, null)

will give your 'myApp'. If this is not what you're asking for, please type in the complete <a> tag that you want to be rendered. As an example:

<a href="http://localhost:30201/myApp/Users/Edit/1">Doe, John</a>

Arun

0
 

Author Comment

by:ToString1
ID: 35211125
Thanks nmarun

Brilliant

Sorry I was causing confusion
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question