开发者

Getting Incorrect razor syntax errors

I have the following code and I want to put the table headers outside of the @foreach but when i move it outside It no longer sees the closing tag. Thanks

@foreach (var item in Model)
{           
<div class="data" ><label for="fullName" >Name: </label>@item.UserName.Replace('.', '')</div>          
if (Model.Count > 0)
{

<table class="data">
<tr>      
    <th>
        Work Date
    </th>
    <th>
        Change Details
    </th>
    <th>
        Ip Address
    </th>
    <th></th>
</tr>


<tr>           
    <td>            
    @{
    var stringDate = item.WorkDate.ToShortDateString(); 
    }
        @Html.DisplayFor(modelItem => stringDate)
    </td>
    <td>
         @Html.DisplayFor(modelItem => item.ChangeDetail)
    </td>    
     <td>
         @Html.DisplayFor(modelItem => item.IpAddress)
    </td>     
</tr>
</table>
}
<br />
<hr />

}

So this is kinda what I'm trying to accomplish, but it keeps giving me errors no matter what way I try it.

<table class="data">
<tr>      
    <th>
        Work Date
    </th>
    <th>
        Change Details
    </th>
    <th>
        Ip Address
    </th>
    <th></th>
</tr>

    @fo开发者_运维技巧reach (var item in Model)
{  

  <div class="data" ><label for="fullName" >Name: </label>@item.UserName.Replace('.','')</div>          
if (Model.Count > 0)
{


  <tr>           
    <td>            
    @{
    var stringDate = item.WorkDate.ToShortDateString(); 
    }
        @Html.DisplayFor(modelItem => stringDate)
    </td>
    <td>
         @Html.DisplayFor(modelItem => item.ChangeDetail)
    </td>    
     <td>
         @Html.DisplayFor(modelItem => item.IpAddress)
    </td>     
</tr>


</table>
}
<br />
<hr />

}


You must have your table closing tag outside of the for each loop.


Short answer: the closing tag </table> has to be placed outside of the foreach loop.


Long answer: You should put the Model.Count > 0 outside of the foreach loop because the moment you enter the code block within the loop, Model.Count > 0 always evaluates to true because you have at least one item.

If – and only if – your model contains any items, you print out the table header. After that, you append one row for each item in Model. Both <table> and </table> have to occur within the same level of nesting.

@if (Model.Count > 0)
{
    <table class="data">
        <tr>      
            <th>Date</th>
            <th>Change Details</th>
            <th>Ip Address</th>
            <th></th>
        </tr>

    @foreach (var item in Model)
    {           
        <div class="data" >
            <label for="fullName" >Name: </label>
            @item.UserName.Replace('.', ' ')
        </div>

        @{
            var stringDate = item.WorkDate.ToShortDateString(); 
        }

        <tr>           
            <td>@Html.DisplayFor(modelItem => stringDate)</td>
            <td>@Html.DisplayFor(modelItem => item.ChangeDetail)</td>    
            <td>@Html.DisplayFor(modelItem => item.IpAddress)</td>     
        </tr>
    }

    </table>

    <br />
    <hr />
}

You should consider moving the logic (@item.UserName.Replace('.', ' ')) out of the view into the corresponding controller action.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜