我正在为我的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命名空间。
答案 0 :(得分:3)
删除引号(你想调用函数,不只是将代码存储在字符串中)和@
(那是Razor,而不是C#)。您可能需要将Html
更改为您(可能)扩展方法中所谓的辅助参数。
此外,Html.ActionLink
已经返回MvcHtmlString
,因此您可以直接将其放在return
之后。