开发者

Html.ActionLink asp.net-MVC

开发者 https://www.devze.com 2023-01-05 08:47 出处:网络
I have companycontroller, index view and I need to create a link to dispaly allcontacts for this company,

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:

  &开发者_StackOverflow社区lt;%: Html.ActionLink("Contacts", "Index/" + item.CompanyID, "Contacts")%>  

thanks


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 }) %>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消