unexpected "{" after @ in razor code in mvc4

user2122825 picture user2122825 · Jan 5, 2015 · Viewed 7.1k times · Source

I am getting below error in render partial in razor code, Unexpected "{" after "@" character. Once inside the body of a code block (@if {}, @{}, etc.) you do not need to use "@{" to switch to code.

@if (Model.Count() > 0)
{
    <div id="mReserveForTodayPartial">    
        @{Html.Partial("UpdateReserveForToday.mobile");}
    </div>
}

kindly help..!

Answer

Ehsan Sajjad picture Ehsan Sajjad · Jan 5, 2015

Html.Partial() return MvcHtmlString so you have to do like this:

 @Html.Partial("UpdateReserveForToday")

in Html.RenderPartial() case it writes to the output stream and that's why it's return type is void, so when using Html.RenderPartial() you have to do this:

@{

Html.RenderPartial("UpdateReserveForToday");

}