Solved

MVC HTML.Actionlink to route to different controller action

Posted on 2011-03-24
14
1,822 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
There is a huge demand for CodeIgniter among the PHP web developers due to its dynamic features and benefits these days. It is one of most popular and agile open source PHP framework for creating robust web applications in PHP web development field.…
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…

808 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