【问题标题】:Create a custom ActionLink创建自定义 ActionLink
【发布时间】:2014-09-10 10:36:32
【问题描述】:

我想创建一个自定义操作链接,但我不知道如何在满足我的需求的同时执行此操作。我之前使用过自定义 htmlhelpers,但这对我来说有点棘手。

我要调用的actionlink需要是这样的:

@Html.CustomActionLink("LinkText", "Area","Controller","TabMenu","Action",routeValues, htmlAttributes)

例如:

 @Html.CustomActionLink("Click here","Travel","Trip","Index","Index", new { par1 = "test", par2 = test2, new { @class = "font-color-blue" })`

会生成这个 html:

<a class="font-color-blue" href="/Trip/Travel/Index/Index?par1=test&par2=test2">Click Here</a>

我的路线如下:

 context.MapRoute(
            "EPloeg_default",
            "EPloeg/{controller}/{tabmenu}/{action}/{id}/{actionMethod}",
            new { action = "Index", id = UrlParameter.Optional, actionMethod = UrlParameter.Optional }
        );   

有什么办法可以做到这一点吗?

【问题讨论】:

  • 那会生成什么 html?
  • 不好意思忘记了,在问题里加了!

标签: asp.net-mvc asp.net-mvc-4 html-helper actionlink


【解决方案1】:

下面的代码怎么样,

@Html.ActionLink("Click here","Trip","Index", new { area= "Travel", tabmenu= "Index"}, new { @class = "font-color-blue" })

编辑

你可以使用这样的扩展方法,

public static MvcHtmlString CustomActionLink(this HtmlHelper htmlHelper, string linkText, string area, string controller, string tabMenu, string action, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
    routeValues.Add("area", area);
    routeValues.Add("tabMenu", tabMenu);
    return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
}

【讨论】:

  • 我希望它更干净,这就是为什么当我有很多参数时,我的路由值中不需要 area 和 tabmenu。
  • 那么只需创建一个扩展方法并在其中执行此操作
  • 检查我的编辑以获取示例扩展方法
  • 显然参数没有正确传递。这就是我调用 actionlink 的方式:@Html.CustomActionLink(EnumTranslator.Translate(item.TypeEvaluation), "EPloeg", "Evaluation", Model.TabMenu, "Detail", new RouteValueDictionary(new { id = item.Id, actionMethod = 模型.ActionMethod }))
  • 如果您传递所有string 值,它是否正确传递?并检查哪些值没有通过
【解决方案2】:

您可以实现自定义动作链接扩展,您必须在LinkExtensions 类中编写自己的方法:

namespace TestCustomHelper.Html
{

public static class LinkExtensions
{
public static MvcHtmlString ActionLinkAuthorized(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes, bool showActionLinkAsDisabled)
  {
     if (htmlHelper.ActionAuthorized(actionName, controllerName))
     {
       return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
     }
     return MvcHtmlString.Empty;
}

}

并在视图中使用它:

@using TestCustomHelper.Html


@Html.ActionLinkAuthorized("Create New", "Create", new { org = ViewBag.OrgBranchID }, new { @id = "linkCreateEmployee" },true) 

注意:

我已经添加了一个额外的bool参数,如果您看到方法的最后一个参数,您可以根据需要添加更多。

我只是根据我的需要为它写了一个重载,你可以把所有的重载都写成Html.ActionLink()有。

见我的tutorial on Creating Custom Html Helper Extensions in asp.net mvc

另见Official asp.net mvc Creating Custom HTML Helpers

更新

你可能想看看我的this answer in which custom Action Link I wrote

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 2020-06-01
    • 2013-08-29
    • 2010-12-01
    • 1970-01-01
    相关资源
    最近更新 更多