2015-05-20 50 views
34

Html.ActionLink()中的问题是,您无法在其生成的标记内添加其他HTML内容。 例如,如果你想添加一个图标,除了像文字:如何在Url.Action中传递区域?

<a href="/Admin/Users"><i class="fa fa-users"></i> Go to Users</a> 

使用Html.ActionLink(),只能生成:

<a href="/Admin/Users">Go to Users</a> 

因此,要解决这个问题,你可以使用Url.Action()只产生像标签里面的网址:

// Here, Url.Action could not generate the URL "/admin/users". So this doesn't work. 
<a href="@Url.Action("", "Users", "Admin")"><i class="fa fa-usesr"></i> Go to Users</a> 

// This works, as we know it but won't pass the Area needed. 
<a href="@Url.Action("", "Users")"><i class="fa fa-users"></i> Go to Users</a> 

那么,你如何通过使用Url.Action面积()?

非常感谢您提前!

+12

'Url.Action(“actionName”,“controllerName”,new {Area =“areaName”});' – haim770

+2

对于根区'new {Area =“”}' – Corio

回答

56

您可以使用此Url.Action("actionName", "controllerName", new { Area = "areaName" });

也不要忘了添加控制器的命名空间,以避免管理区控制器名称和站点控制器名称之间的冲突。

像这样的事情

public override void RegisterArea(AreaRegistrationContext context) 
     { 
      context.MapRoute(
       "Admin_default", 
       "Admin/{controller}/{action}/{id}", 
       new { action = "Index", id = UrlParameter.Optional }, 
        new[] { "Site.Mvc.Areas.Admin.Controllers" } 
      ); 
     } 
2
@Url.Action("{action}", "{controller}", new { Area = "areaname" }); 
@Html.ActionLink("LinkName", "{action}", "{controller}", new { area = "{areaname}" }, new { @class = "btn btn-cool" }) 

写入区域名称与anonymus对象HTML属性。你可以使用actionlink html helper扩展方法来实现同样的事情。

相关问题