我正在为我的MVC 3项目编写一个Html助手。
我想返回MvcHtmlString,比如"@Html.ActionLink(xxxxx)",我应该写什么?
目前我有这个代码
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命名空间。
发布于 2012-04-28 23:20:08
删除引号(您希望调用函数,而不仅仅是将代码存储在字符串中)和@ (即Razor,而不是C# )。您可能需要将Html更改为(想必)扩展方法中称为助手参数的任何东西。
而且,Html.ActionLink已经返回了MvcHtmlString,所以您可以将它直接放在return之后。
https://stackoverflow.com/questions/10368406
复制相似问题