2012-04-28 25 views
0

我正在爲我的MVC 3項目編寫一個Html Helper。如何返回@ Html.ActionLink

我想返回像「@ 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); 
    } 

它返回整個字符串....但我想呈現的字符串。所以我該怎麼做?感謝大家。

UPDATE

貌似我可以返回此直接

請參見下面的代碼

 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命名空間。

回答

3

刪除引號(你想調用的函數,不只是存儲在一個字符串中的代碼)和@(這是剃刀,而不是C#反正)。您可能需要將Html更改爲您的(可能的)擴展方法中所稱的幫助程序參數。

另外,Html.ActionLink已經返回MvcHtmlString所以你可以直接在return之後。

+0

非常感謝,我會試一試。 – HorseKing 2012-04-28 23:24:04