脚手架生成的视图代码中的 HttpParseException
HttpParseException in scaffolding-generated view code
我在 ASP.NET MVC5 中使用以下代码得到以下异常。我使用内置的脚手架来生成我的视图代码 (List),但它不起作用。异常是关于一个 else 块,但它被抛出在我的 foreach 中的第二个元素,所以我完全不知道发生了什么(这里:@Html.DisplayFor(modelItem => item.Nem)
)。
我读到了这个异常,但那些案例实际上是关于 if-else 的案例,所以我不能使用那个解决方案,而且仍然不知道我做错了什么。
System.Web.HttpParseException: 'The else block is missing a closing
"}" character. Make sure you have a matching "}" character for all
the "{" characters within this block, and that none of the "}"
characters are being interpreted as markup.'
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Nev)
</td>
<td>
@Html.DisplayFor(modelItem => item.Nem)
</td>
<td>
@Html.DisplayFor(modelItem => item.SzuletesiIdo)
</td>
<td>
@Html.DisplayFor(modelItem => item.SzuletesiHely)
</td>
<td>
@Html.DisplayFor(modelItem => item.Taj)
</td>
<td>
@Html.DisplayFor(modelItem => item.Irsz)
</td>
<td>
@Html.DisplayFor(modelItem => item.Telepules)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cim)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Mobil)
</td>
<td>
@Html.DisplayFor(modelItem => item.OtthoniTel)
</td>
<td>
@Html.DisplayFor(modelItem => item.AnyaNev)
</td>
<td>
@Html.DisplayFor(modelItem => item.AnyaMobil)
</td>
<td>
@Html.DisplayFor(modelItem => item.AnyaEmail)
</td>
<td>
@Html.DisplayFor(modelItem => item.ApaNev)
</td>
<td>
@Html.DisplayFor(modelItem => item.ApaMobil)
</td>
<td>
@Html.DisplayFor(modelItem => item.ApaEmail)
</td>
<td>
@Html.DisplayFor(modelItem => item.TanfolyamSorszam)
</td>
<td>
@Html.DisplayFor(modelItem => item.TanfolyamOsztaly)
</td>
<td>
@Html.DisplayFor(modelItem => item.Csoport)
</td>
<td>
@Html.DisplayFor(modelItem => item.BallagasEv)
</td>
<td>
@Html.DisplayFor(modelItem => item.KimaradtDatum)
</td>
<td>
@Html.DisplayFor(modelItem => item.PoloMeret)
</td>
<td>
@Html.DisplayFor(modelItem => item.Iskola)
</td>
<td>
@Html.DisplayFor(modelItem => item.BkkIgazolvany)
</td>
<td>
@Html.DisplayFor(modelItem => item.MavIgazolvany)
</td>
<td>
@Html.DisplayFor(modelItem => item.MakIgazolvany)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProgramUtanHaza)
</td>
<td>
@Html.DisplayFor(modelItem => item.EgeszsegugyiTudnivalo)
</td>
<td>
@Html.DisplayFor(modelItem => item.Egyeb)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.GyerekId }) |
@Html.ActionLink("Details", "Details", new { id=item.GyerekId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.GyerekId })
</td>
</tr>
}
编辑:item.Nem 是一个枚举类型(有两个可能的值)。我使用 DisplayTemplate 来显示枚举 (Views\Shared\DisplayTemplates\Enum.cshtml):
@model Enum
@if (EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata))
{
// Display Enum using same names (from [Display] attributes) as in editors
string displayName = null;
foreach (SelectListItem item in EnumHelper.GetSelectList(ViewData.ModelMetadata, (Enum)Model))
{
if (item.Selected)
{
displayName = item.Text ?? item.Value;
}
}
// Handle the unexpected case that nothing is selected
if (String.IsNullOrEmpty(displayName))
{
if (Model == null)
{
displayName = String.Empty;
}
else
{
displayName = Model.ToString();
}
}
@Html.DisplayTextFor(model => displayName)
}
else
{
// This Enum type is not supported. Fall back to the text.
@Html.DisplayTextFor(model => model)
如果您查看第二个代码块 (Views\Shared\DisplayTemplates\Enum.cshtml):
You are missing the closing }
at the end of your else
statement
这正是异常告诉您的内容:)
我在 ASP.NET MVC5 中使用以下代码得到以下异常。我使用内置的脚手架来生成我的视图代码 (List),但它不起作用。异常是关于一个 else 块,但它被抛出在我的 foreach 中的第二个元素,所以我完全不知道发生了什么(这里:@Html.DisplayFor(modelItem => item.Nem)
)。
我读到了这个异常,但那些案例实际上是关于 if-else 的案例,所以我不能使用那个解决方案,而且仍然不知道我做错了什么。
System.Web.HttpParseException: 'The else block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.'
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Nev)
</td>
<td>
@Html.DisplayFor(modelItem => item.Nem)
</td>
<td>
@Html.DisplayFor(modelItem => item.SzuletesiIdo)
</td>
<td>
@Html.DisplayFor(modelItem => item.SzuletesiHely)
</td>
<td>
@Html.DisplayFor(modelItem => item.Taj)
</td>
<td>
@Html.DisplayFor(modelItem => item.Irsz)
</td>
<td>
@Html.DisplayFor(modelItem => item.Telepules)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cim)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Mobil)
</td>
<td>
@Html.DisplayFor(modelItem => item.OtthoniTel)
</td>
<td>
@Html.DisplayFor(modelItem => item.AnyaNev)
</td>
<td>
@Html.DisplayFor(modelItem => item.AnyaMobil)
</td>
<td>
@Html.DisplayFor(modelItem => item.AnyaEmail)
</td>
<td>
@Html.DisplayFor(modelItem => item.ApaNev)
</td>
<td>
@Html.DisplayFor(modelItem => item.ApaMobil)
</td>
<td>
@Html.DisplayFor(modelItem => item.ApaEmail)
</td>
<td>
@Html.DisplayFor(modelItem => item.TanfolyamSorszam)
</td>
<td>
@Html.DisplayFor(modelItem => item.TanfolyamOsztaly)
</td>
<td>
@Html.DisplayFor(modelItem => item.Csoport)
</td>
<td>
@Html.DisplayFor(modelItem => item.BallagasEv)
</td>
<td>
@Html.DisplayFor(modelItem => item.KimaradtDatum)
</td>
<td>
@Html.DisplayFor(modelItem => item.PoloMeret)
</td>
<td>
@Html.DisplayFor(modelItem => item.Iskola)
</td>
<td>
@Html.DisplayFor(modelItem => item.BkkIgazolvany)
</td>
<td>
@Html.DisplayFor(modelItem => item.MavIgazolvany)
</td>
<td>
@Html.DisplayFor(modelItem => item.MakIgazolvany)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProgramUtanHaza)
</td>
<td>
@Html.DisplayFor(modelItem => item.EgeszsegugyiTudnivalo)
</td>
<td>
@Html.DisplayFor(modelItem => item.Egyeb)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.GyerekId }) |
@Html.ActionLink("Details", "Details", new { id=item.GyerekId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.GyerekId })
</td>
</tr>
}
编辑:item.Nem 是一个枚举类型(有两个可能的值)。我使用 DisplayTemplate 来显示枚举 (Views\Shared\DisplayTemplates\Enum.cshtml):
@model Enum
@if (EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata))
{
// Display Enum using same names (from [Display] attributes) as in editors
string displayName = null;
foreach (SelectListItem item in EnumHelper.GetSelectList(ViewData.ModelMetadata, (Enum)Model))
{
if (item.Selected)
{
displayName = item.Text ?? item.Value;
}
}
// Handle the unexpected case that nothing is selected
if (String.IsNullOrEmpty(displayName))
{
if (Model == null)
{
displayName = String.Empty;
}
else
{
displayName = Model.ToString();
}
}
@Html.DisplayTextFor(model => displayName)
}
else
{
// This Enum type is not supported. Fall back to the text.
@Html.DisplayTextFor(model => model)
如果您查看第二个代码块 (Views\Shared\DisplayTemplates\Enum.cshtml):
You are missing the closing
}
at the end of yourelse
statement
这正是异常告诉您的内容:)