Dhwani
Dhwani

Reputation: 7626

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

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"

Upvotes: 5

Views: 18022

Answers (3)

Mojtaba Homayoun
Mojtaba Homayoun

Reputation: 57

use this code instead :

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

</div>

Upvotes: -1

Vlad Bezden
Vlad Bezden

Reputation: 89527

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

Upvotes: 5

Nitin Varpe
Nitin Varpe

Reputation: 10694

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

Upvotes: 10

Related Questions