How to correctly use Html.ActionLink with ASP.NET MVC 4 Areas

后端 未结 4 924
野的像风
野的像风 2021-02-01 01:30

I recently discovered Areas in ASP.NET MVC 4, which I have successfully implemented, but I\'m running into troubles with the @Html.ActionLink helper in my Razor vie

相关标签:
4条回答
  • 2021-02-01 02:07

    Just to add up my bit:
    Remember, you're gonna need to have at least 2 areas in your MVC application to get the routeValues: { area="" } working; otherwise the area value will be used as a query-string parameter and you link will look like this: /?area=

    If you don't have at least 2 areas, you can fix this behavior by:
    1. editing the default route in RouteConfig.cs like this:

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

    OR
    2. Adding a dummy area to your MVC project.

    0 讨论(0)
  • 2021-02-01 02:17

    I hate answering my own question, but @Matt Bodily put me on the right track.

    The @Html.Action method actually invokes a controller and renders the view, so that wouldn't work to create a snippet of HTML in my case, as this was causing a recursive function call resulting in a StackOverflowException. The @Url.Action(action, controller, { area = "abc" }) does indeed return the URL, but I finally discovered an overload of Html.ActionLink that provided a better solution for my case:

    @Html.ActionLink("Admin", "Index", "Home", new { area = "Admin" }, null)
    

    Note: , null is significant in this case, to match the right signature.

    Documentation: @Html.ActionLink (LinkExtensions.ActionLink)

    Documentation for this particular overload:

    LinkExtensions.ActionLink(Controller, Action, Text, RouteArgs, HtmlAttributes)

    It's been difficult to find documentation for these helpers. I tend to search for "Html.ActionLink" when I probably should have searched for "LinkExtensions.ActionLink", if that helps anyone in the future.

    Still marking Matt's response as the answer.

    Edit: Found yet another HTML helper to solve this:

    @Html.RouteLink("Admin", new { action = "Index", controller = "Home", area = "Admin" })
    
    0 讨论(0)
  • 2021-02-01 02:23

    Below are some of the way by which you can create a link button in MVC.

    @Html.ActionLink("Admin", "Index", "Home", new { area = "Admin" }, null)  
    @Html.RouteLink("Admin", new { action = "Index", controller = "Home", area = "Admin" })  
    @Html.Action("Action", "Controller", new { area = "AreaName" })  
    @Url.Action("Action", "Controller", new { area = "AreaName" })  
    <a class="ui-btn" data-val="abc" href="/Home/Edit/ANTON">Edit</a>  
    <a data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#CustomerList" href="/Home/Germany">Customer from Germany</a>  
    <a data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#CustomerList" href="/Home/Mexico">Customer from Mexico</a> 
    

    Hope this will help you.

    0 讨论(0)
  • 2021-02-01 02:24

    How I redirect to an area is add it as a parameter

    @Html.Action("Action", "Controller", new { area = "AreaName" })
    

    for the href portion of a link I use

    @Url.Action("Action", "Controller", new { area = "AreaName" })
    
    0 讨论(0)
提交回复
热议问题