"\"@\" is not valid at the start of a code block

15,399

Solution 1

I think the problem is in id="item@@name". You can't have @@ there. Razor is using @ character. In cases where the content is valid as code as well (and you want to treat it as content), you can explicitly escape out @ characters by typing @@. So in your case you will have id="item@name" after Razor parse it.

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

If you need to use variable inside please use

<div id="item@(name)" class='itemDivs'>

Solution 2

I have read somewhere that @@ works in mvc3 i.e. razor 1.0. But somehow doesnt work in mvc4 razor-2.0

So try Changing

<div id="item@@name" class='itemDivs'>

to

<div id="item@("@name")" class='itemDivs'>
Share:
15,399

Related videos on Youtube

Dhwani
Author by

Dhwani

Software Engineer, India.

Updated on June 04, 2022

Comments

  • Dhwani
    Dhwani almost 2 years

    Here is my razor view.

    @model IEnumerable<ApricaROI.Models.DatabaseEntities.SalesItemMapping>
    @{
        var name_List = Model.GroupBy(x => x.Name).Select(y => y.First()).ToList();
    }
    @foreach (var name in Model.Select(item => item.Name).Distinct()) {
        <div id="item@@name" class='itemDivs'>
            @{ Html.RenderPartial("_EditItemChild",
                         Model.Where(item => item.Name== name).ToList()); }
        </div>
    }
    

    I don't know what kind of error it has. It is giving following error.

    "\"@\" is not valid at the start of a code block.
    Only identifiers, keywords, comments, \"(\" and \"{\" are valid.\r\n"