Razor Syntax Missing } character

2019-07-12 09:54发布

问题:

I think this is a school boy error but can you explain to me and provide the fix to why this view won't work:

@{
    int PreviousOrderId = 0;
}


@foreach (var item in Model) {

<div class="orderdetail">


@if (item.ID != PreviousOrderId){

    <div class="customer">
        <p class="strong">OrderID:</p> 
        <p>@item.ID</p>
        <p class="strong">Order Date:</p>
        <p>@String.Format("{0:g}", TimeZoneInfo.ConvertTime(item.DateInitialised, TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time")))</p>
        <p class="strong">Customer Name:</p> 
        <p>@item.WebsiteUser.Name</p>
        <p class="strong">Practice Name:</p>
        <p>@item.WebsiteUser.PracticeName</p>
        <p class="strong">Customer E-Mail:</p> 
        <p>@item.WebsiteUser.EMailAddress</p>
    </div>
   }


    <div class="detail">
        <span class="strong">Licence Key:</span><span><div style="width:140px; overflow:auto;">@item.Licence.LicenceKey</div></span>
        <span class="strong">Serial No:<span><span>@item.Licence.SerialNumber</span>
    </div>               

    <div class="clear></div>
</div>
@{
   PreviousOrderId = item.ID;
}

}

回答1:

there is a missing / near a span-tag, a missing " near the class attribute in the clear-div-tag and the @{ } around PreviousOrderId = item.ID wasn't necessary

@{
    int PreviousOrderId = 0;
}


@foreach (var item in Model) {

<div class="orderdetail">


@if (item.ID != PreviousOrderId){

    <div class="customer">
        <p class="strong">OrderID:</p> 
        <p>@item.ID</p>
        <p class="strong">Order Date:</p>
        <p>@String.Format("{0:g}", TimeZoneInfo.ConvertTime(item.DateInitialised, TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time")))</p>
        <p class="strong">Customer Name:</p> 
        <p>@item.WebsiteUser.Name</p>
        <p class="strong">Practice Name:</p>
        <p>@item.WebsiteUser.PracticeName</p>
        <p class="strong">Customer E-Mail:</p> 
        <p>@item.WebsiteUser.EMailAddress</p>
    </div>
   }


    <div class="detail">
        <span class="strong">Licence Key:</span><span><div style="width:140px; overflow:auto;">@item.Licence.LicenceKey</div></span>
        <span class="strong">Serial No:</span><span>@item.Licence.SerialNumber</span>
    </div>               

    <div class="clear"></div>
</div>

   PreviousOrderId = item.ID;


}