Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The foreach block is missing a closing "}" character

I'm having fun with Razor today! Can you see what is wrong with this view and explain why it errors?

@foreach (var item in Model){

if (item.ID != PreviousOrderId){
    <div class="orderdetail">
        <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>@item.Licence.LicenceKey</span>
        <span class="strong">Serial No:</span><span>@item.Licence.SerialNumber</span>
    </div>               

   @if (item.ID != PreviousOrderId && PreviousOrderId != 0){
    </div>
    <div class="clear"></div>
   }        

PreviousOrderId = item.ID;
}
like image 424
Jon Avatar asked Mar 12 '12 11:03

Jon


2 Answers

You have divided <div class="orderdetail"> into two pieces. Opening tag is in first if block, closing tag is in second if block.

This is something confusing for Razor, I think.

Try to open and close your div in one if block.

like image 97
adyusuf Avatar answered Nov 10 '22 13:11

adyusuf


if you already are inside a code-block you dont need to @-prefix code constructs anymore...your code block starts with @foreach.

@foreach (var x in new string[] { "x", "y" ])
{
    if (userGuid != ViewBag.x)
   {
    @:<div class="orderdetail">
    <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</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>@item.Licence.LicenceKey</span>
        <span class="strong">Serial No:</span><span>@item.Licence.SerialNumber</span>
    </div>               

if (userGuid != ViewBag.x && ViewBag.x != 0)
{
    @:</div>
    <div class="clear"></div>
}      
PreviousOrderId = item.ID;    
}
like image 35
360Airwalk Avatar answered Nov 10 '22 13:11

360Airwalk