C# C语言命令的Razor语法

C# C语言命令的Razor语法,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我试图在引导网格中的每行显示4个产品 除一行代码(@i=i-1;)外,此代码在视图中按预期运行 @for(int i=0;i只需在4个块上进行迭代: @for (int i = 0; i < Model.Count() / 4; i++) { if (i < (Model.Count() / 4) || (4 * i) < Model.Count()) { <div class="row"> @for (int j = 0;

我试图在引导网格中的每行显示4个产品

除一行代码(
@i=i-1;
)外,此代码在
视图中按预期运行


@for(int i=0;i只需在4个块上进行迭代:

@for (int i = 0; i < Model.Count() / 4; i++)
{
    if (i < (Model.Count() / 4) || (4 * i) < Model.Count())
    {
    <div class="row">
        @for (int j = 0; j < 4; j++)
        {

                <div class="col-md-3">
                    <h2>@Model[(i * 4) + j].ItemName</h2>
                    <br />
                    <button style="border:none; padding:0;" OnClick="window.location.href='@Url.Action("Details", "Products", new { id = Model[(i * 4) + j].ItemID })'"><img src="@Model[(i * 4) + j].imageUrl" , width="100" height="75" /></button><br /><br />
                    <p>
                        Price: @Model[(i * 4) + j].ItemPrice
                        Availability:<span style="color:green; font-weight:bold;">Yes</span><br />
                    </p>
                    <p>
                        <button class="btn btn-default" OnClick="window.location.href='@Url.Action("Details", "Products", new { id = Model[(i * 4) + j].ItemID })'">Learn more &raquo;</button>
                        <span style="margin-left:140px">@Html.ActionLink("Buy >>", "Index", "ShoppingCart", new { area = "" }, new { @class = "btn btn-primary btn-lg" })</span>
                    </p>
                </div>

        }

    </div>
    }
} 
for(int i=0;i

价格:@Model[(i*4)+j].ItemPrice 可用性:是

了解更多信息»; @ActionLink(“购买>>”,“索引”,“购物车”,新{area=”“},新{@class=“btn btn primary btn lg”})

} } }

**请注意,如果要在引导行中放置4个元素,则应使用col-md-3而不是4!

使用此代码在引导网格中的每行显示4个产品

   @{
  int index = 0;
 }
 @foreach (var item in @Model)
 {
   if (index % 4 == 0)
   {
      @:  <div class="row">
   }

   <div class="col-md-3">

     <h2>@item.ItemName</h2>
            <br />
            <button style="border:none; padding:0;" OnClick="window.location.href='@Url.Action("Details", "Products", new { id = item.ItemID })'"><img src="@item.imageUrl",  width="100" height="75" /></button><br /><br />
            <p>
                Price: @item.ItemPrice
                Availability:<span style="color:green; font-weight:bold;">Yes</span><br />
            </p>
            <p>
                <button class="btn btn-default" OnClick="window.location.href='@Url.Action("Details", "Products", new { id = item.ItemID })'">Learn more &raquo;</button>
                <span style="margin-left:140px">@Html.ActionLink("Buy >>", "Index", "ShoppingCart", new { area = "" }, new { @class = "btn btn-primary btn-lg" })</span>
            </p>
   </div>

   if (index % 4 == 0)
   {
      @:     </div>
   }        

   index++;
}
@{
int指数=0;
}
@foreach(@Model中的var项)
{
如果(索引%4==0)
{
@:  
}
@item.ItemName



价格:@item.ItemPrice 可用性:是

了解更多信息»; @ActionLink(“购买>>”,“索引”,“购物车”,新{area=”“},新{@class=“btn btn primary btn lg”})

如果(索引%4==0) { @: } 索引++; }
您不能分配@variable。我认为它是只读的。您需要使用@I=I-1;},因为@I是回音或打印输出。您可能不应该在for循环中使用@in。否则这里就需要使用语句。您实际想用
I++;
@{I=I-1;}
行代码做什么?如果(我最好的猜测是您正在尝试创建一个4列布局?(在这种情况下,有更好的方法可以做到)@StephenMuecke-
View
接收产品列表(模型)。我试图在浏览器中的每行显示4个产品。然后参考如何执行此操作。实际上,如果J参数小于4,则您应该知道J参数,例如,如果您有5个元素,且I变量为1,则在第二次迭代中J只有1条记录。
   @{
  int index = 0;
 }
 @foreach (var item in @Model)
 {
   if (index % 4 == 0)
   {
      @:  <div class="row">
   }

   <div class="col-md-3">

     <h2>@item.ItemName</h2>
            <br />
            <button style="border:none; padding:0;" OnClick="window.location.href='@Url.Action("Details", "Products", new { id = item.ItemID })'"><img src="@item.imageUrl",  width="100" height="75" /></button><br /><br />
            <p>
                Price: @item.ItemPrice
                Availability:<span style="color:green; font-weight:bold;">Yes</span><br />
            </p>
            <p>
                <button class="btn btn-default" OnClick="window.location.href='@Url.Action("Details", "Products", new { id = item.ItemID })'">Learn more &raquo;</button>
                <span style="margin-left:140px">@Html.ActionLink("Buy >>", "Index", "ShoppingCart", new { area = "" }, new { @class = "btn btn-primary btn-lg" })</span>
            </p>
   </div>

   if (index % 4 == 0)
   {
      @:     </div>
   }        

   index++;
}