我正在構建一個htmlhelper擴展,但出現此錯誤:無效的匿名類型成員聲明。匿名...?
無效的匿名類型成員聲明符。匿名類型成員必須聲明 與成員分配,簡單名稱或成員訪問。
我試圖@ User.IsInRole轉換爲一個布爾值,但無濟於事:(
這是剃刀標記:
@using htmlHelperstring.Models
@{
ViewBag.Title = "Home Page";
}
<ul>
@Html.MyActionLink(
"<span>Hello World</span>",
"about",
"home",
new { id = "123" },
new { @class = "foo",(bool)(@User.IsInRole("Chef"))}
)
</ul>
幫手:
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace htmlHelperstring.Models
{
public static class LabelExtensions
{
public static IHtmlString MyActionLink(
this HtmlHelper htmlHelper,
string linkText,
string action,
string controller,
object routeValues,
object htmlAttributes,
bool UserAuthorized
)
{
var li = new TagBuilder("li");
if (UserAuthorized)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var anchor = new TagBuilder("a");
anchor.InnerHtml = linkText;
anchor.Attributes["href"] = urlHelper.Action(action, controller, routeValues);
anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));
li.InnerHtml = anchor.ToString();
}
else
{
li.InnerHtml = string.Empty;
}
return MvcHtmlString.Create(li.ToString());
}
}
}