Solved

MVC HTML.Actionlink to route to different controller action

Posted on 2011-03-24
14
1,812 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

19 Experts available now in Live!

Get 1:1 Help Now