0

I have companycontroller, index view and I need to create a link to dispaly all contacts for this company,

this is working but YUK, How can Refactor this:

  <%: Html.ActionLink("Contacts", "Index/" + item.CompanyID, "Contacts")%>  

thanks

1
  • That works? Wow, news to me. Commented Jun 30, 2010 at 3:13

2 Answers 2

2

Assuming you are using the default routes:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

If you need to specify the controller name try this (don't forget the last parameter - the html attributes which I am passing to null here):

<%: Html.ActionLink("Contacts", "Index", "Contacts", new { id = item.CompanyID }, null) %>

or without specifying the controller (using the current controller):

<%: Html.ActionLink("Contacts", "Index", new { id = item.CompanyID }) %>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks it worked: <%: Html.ActionLink("Contacts", "Index", "Contacts", new { id = item.CompanyID }, null) %>
0

Thats pretty... different...

Normally it would be:

<%: Html.ActionLink("Contacts", "Index", "Contacts", new { item.CompanyID } )%>

with the last parameter being a anonymous object which gets translated into a routevalue dictionary.

3 Comments

it gives me : localhost:14501/companies?Length=8, my current url is: localhost:14501/companies and I need to have this: localhost:14501/Contacts/Index/4 7 is the company ID
It must be new {id=item.CompanyID}
@Malcom Frexner - Not necessarily. I was assuming his route value was CompanyID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.