Search code examples
c#asp.net-mvcrazorasp.net-mvc-scaffolding

HttpParseException in scaffolding-generated view code


I get the following exception with the following code in ASP.NET MVC5. I used the built-in scaffolding to generate my view code (List), and it just doesn't work. The exception is about an else block, but it is thrown at the second element in my foreach so I have absolutely no idea what is going on (here: @Html.DisplayFor(modelItem => item.Nem)).

I read about this exception but that cases were really about an if-else case so I can't use that solutions and still don't know what I have done wrong.

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

EDIT: item.Nem is an enum type (with two possible values). I use a DisplayTemplate for displaying enums (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)

Solution

  • If you look at your second code block (Views\Shared\DisplayTemplates\Enum.cshtml):

    You are missing the closing } at the end of your else statement

    Which is exactly what the exception is telling you :)