创建自定义的ActionLink [英] Create a custom ActionLink

查看:184
本文介绍了创建自定义的ActionLink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义的ActionLink,但我不知道如何做到这一点,同时满足我的需求。我自定义htmlhelpers合作过,但这是比较麻烦一些,适合我。

I want to create a custom actionlink but I don't know how to do this while fulfilling my needs. I worked with custom htmlhelpers before but this is a bit more tricky for me.

ActionLink的,我想打电话给需要是这样的:

The actionlink that I want to call needs to be like this:

@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" })`

这将产生这个网站:

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

任何想法我怎么能做出这样?

Any ideas how I can make this?

推荐答案

如何以下code,

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


修改

您可以使用扩展方法就是这样,

You can use a extension method like this,

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

这篇关于创建自定义的ActionLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆