Solved

MVC HTML.Actionlink to route to different controller action

Posted on 2011-03-24
14
1,842 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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.
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

734 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