如何返回@ Html.ActionLink

时间:2012-04-28 23:18:24

标签: asp.net-mvc-3 html-helper

我正在为我的MVC 3项目编写一个Html Helper。

我想返回像“@ Html.ActionLink(xxxxx)”这样的MvcHtmlString,我该怎么写?

目前我有这段代码

        public static MvcHtmlString SetFeaturedFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel, TValue>> expression)
    {
        var isFeatured =Convert.ToBoolean(ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model.ToString());

        string result = "Html.ActionLink(Delete, DeleteComment, Admin, new { Id = @thisComment.CommentId }, null)";

        return MvcHtmlString.Create(result);
    }

它返回整个字符串....但我想要渲染的字符串。所以我该怎么做?谢谢大家。

更新

看起来我可以直接退回

见下面的代码

        public static MvcHtmlString SetFeaturedFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel, TValue>> expression)
    {
        var isFeatured =Convert.ToBoolean(ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model.ToString());

        string indicatorText = (isFeatured) ? "Unset Featured" : "Set Featured";

        return htmlHelper.ActionLink(indicatorText, "SetFeaturedIncident", "Admin", null, null);
    }

需要导入System.Web.Routing命名空间。

1 个答案:

答案 0 :(得分:3)

删除引号(你想调用函数,不只是将代码存储在字符串中)和@(那是Razor,而不是C#)。您可能需要将Html更改为您(可能)扩展方法中所谓的辅助参数。

此外,Html.ActionLink已经返回MvcHtmlString,因此您可以直接将其放在return之后。