开发者

Razor renderpartial exception - expected '}"

In my code:

@foreach (var post in Model.Posts)
{
    Html.RenderPartial("ShowPostPartial", post);
}

i have an excepion on RenderPar开发者_开发知识库tial line.

error CS1513: } expected.

What am I doing wrong?


For completeness, here's another way of causing this:

@if(condition)
{
    <input type="hidden" value="@value">
}

The problem is that the unclosed element makes it not obvious enough that the content is an html block (but we aren't always doing xhtml, right?).

In this scenario, you can use:

@if(condition)
{
    @:<input type="hidden" value="@value">
}

or

@if(condition)
{
    <text><input type="hidden" value="@value"></text>
}


This is basically the same answer that Mark Gravell gave, but I think this one is an easy mistake to make if you have a larger view: Check the html tags to see where they start and end, and notice razor syntax in between, this is wrong:

@using (Html.BeginForm())
{ 
   <div class="divClass">
     @Html.DisplayFor(c => c.SomeProperty)
}
</div>

And this is correct:

@using (Html.BeginForm())
{ 
   <div class="divClass">
     @Html.DisplayFor(c => c.SomeProperty)
   </div>
}

Again, almost same as the earlier post about unclosed input element, but just beware, I've placed div's wrong plenty of times when changing a view.


MY bad. I've got an error in the partial view. I've written 'class' instead of '@class' in htmlAttributes.


I've gotten this issue with Razor. I'm not sure if it's a bug in the parser or what, but the way I've solved it is to break up the:

@using(Html.BeginForm()) { 
<h1>Example</h1>
@foreach (var post in Model.Posts)
{
    Html.RenderPartial("ShowPostPartial", post);
}

} 

into:

@{ Html.BeginForm(); }
<h1>Example</h1>
@foreach (var post in Model.Posts)
{
    Html.RenderPartial("ShowPostPartial", post);
}

@{ Html.EndForm(); }


The Razor parser of MVC4 is different from MVC3. Razor v3 is having advanced parser features and on the other hand strict parsing compare to MVC3.

--> Avoid using server blocks in views unless there is variable declaration section.

Don’t : \n @{if(check){body}} Recommended : @if(check){body}

--> Avoid using @ when you are already in server scope.

Don’t : @if(@variable) Recommended : @if(variable)

Don't : @{int a = @Model.Property } Recommended : @{int a = Model.Property }

Refrence : https://code-examples.net/en/q/c3767f

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜