unexpected “{” after @ in razor code in mvc4

2019-08-08 07:49发布

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..!

3条回答
戒情不戒烟
2楼-- · 2019-08-08 08:04

Below code should resolve this issue.

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

As Razor view engine can parse the code if the statements are available with in "@{ }", So in your code "@{}" is present at "If" statement so its not required to specify again.

查看更多
迷人小祖宗
3楼-- · 2019-08-08 08:06

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");

}
查看更多
小情绪 Triste *
4楼-- · 2019-08-08 08:31

try this code:

@{  List<SelectListItem> listItems = new List<SelectListItem>();
  foreach (var item in ViewData["Subcategory"] as IEnumerable<ApplicationOneStoreForEstore.Models.tblsubcategory>)     {
    listItems.Add(new SelectListItem            {
      Text = item.subcategory_name,
      Value = Convert.ToString(item.subcategory_id)
    });
  }
}

If you have to bind DropDownList then You have to placed this code before Html.BeginForm(..)

查看更多
登录 后发表回答