Pindakaas
Pindakaas

Reputation: 4439

Invalid anonymous type member declarator. Anonymous ...?

I am building an htmlhelper extension but getting this error:

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

I tried to cast the @User.IsInRole to a boolean but to no avail:(

this is the Razor markup:

@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>

helper:

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());
        }
    }
}

Upvotes: 0

Views: 3083

Answers (3)

Pindakaas
Pindakaas

Reputation: 4439

I just made a stupid typo(end of the day etc ) it should be:

@{ 
    ViewBag.Title = "Home Page"; 
    myID = new { id = 123 }; 
    myClass = new { @class = "foo"},(bool)(@User.IsInRole("Chef")) 
} 

Upvotes: 0

Chris Trahey
Chris Trahey

Reputation: 18290

I don't write asp.net (never a single line, actually; so take it with a grain of salt); but I would suspect that the construct:

new { id = "123" }

(and the similar one beneath it) is what the message is referring to by "Anonymous type", and I have a few thoughts on why what you have might be erroneous (with the third feeling most likely).

First, If it's anything like C-style structs, you may need to use a '.' preceding the "member" identifier (which, to me, makes sense for that error message):

new { .id = "123}

Second, the wording of that error makes me wonder if in this environment you aren't allowed to pass in an anonymous object like that; and you need to assign it to a variable first and then pass the variable. Forgive any syntax errors:

@using htmlHelperstring.Models
@{
    ViewBag.Title = "Home Page";
    myID = new { id = 123 };
    myClass = new { @class = "foo",(bool)(@User.IsInRole("Chef"))}
}

<ul>
    @Html.MyActionLink(
    "<span>Hello World</span>", 
    "about", 
    "home",
    myID,
    myClass
)
</ul>

Third, the syntax of new { @class = "foo",(bool)(@User.IsInRole("Chef"))} looks wonky to me. Perhaps (note adding a member name): new { @class = "foo", isChef = (bool)(@User.IsInRole("Chef"))}

Upvotes: 0

Nathalie Kellenberger
Nathalie Kellenberger

Reputation: 341

Looks like you are missing a member assignment in:

new { @class = "foo",(bool)(@User.IsInRole("Chef"))} 

What do you want to assign the boolean to?

You need something like this:

new { @class = "foo", HTMLATTRIBUTENAME = (bool)(@User.IsInRole("Chef"))} 

Replace the HTMLATTRIBUTENAME with the attribute name you want to be set.

Upvotes: 1

Related Questions