Solved

MVC HTML.Actionlink to route to different controller action

Posted on 2011-03-24
14
1,794 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
This video discusses moving either the default database or any database to a new volume.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now