Asp.net 需要有关购物车索引视图中Razor语法的帮助(Razor视图引擎)

Asp.net 需要有关购物车索引视图中Razor语法的帮助(Razor视图引擎),asp.net,asp.net-mvc,asp.net-mvc-3,razor,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Razor,刚开始使用时,我很喜欢它,感觉比webforms等查看引擎要简单得多, 但是当我进一步使用它时,我不得不注意到它对“{”括号的位置和其他场景非常敏感。它会在旧的视图引擎没有那么挑剔的地方出现错误 例如,下面的代码将产生一个错误,因为表单帮助程序结束括号} 位于标记下。如果我把它放在上方,它会工作!但我不需要 这是因为提交按钮输入必须嵌套在其中,我不想将按钮输入放入表中 @model CartTest.Models.Cart @{ ViewBag.Title = "Index"; }

刚开始使用时,我很喜欢它,感觉比webforms等查看引擎要简单得多, 但是当我进一步使用它时,我不得不注意到它对“{”括号的位置和其他场景非常敏感。它会在旧的视图引擎没有那么挑剔的地方出现错误

例如,下面的代码将产生一个错误,因为表单帮助程序结束括号} 位于
标记下。如果我把它放在
上方,它会工作!但我不需要 这是因为提交按钮输入必须嵌套在其中,我不想将按钮输入放入表中

@model CartTest.Models.Cart

@{
    ViewBag.Title = "Index";
}

<h2>Cart Index</h2>

<table width="80%" align="center">
  <thead>
    <tr>
      <th align="center">Quantity</th>
      <th align="left">Item</th>
      <th align="right">Price</th>
      <th align="right">Subtotal</th>
    </tr>
  </thead>
  <tbody>
  @{int index = 0;}
  @using (Html.BeginForm("UpdateCart","Cart"))
  {
    foreach (var line in Model.Lines)
    {
      <tr>
        @Html.Hidden("Lines.Index", index)
        <td align="center">@Html.TextBox("Lines[" + index + "].Quantity", line.Quantity)</td>
        <td align="left">@line.Product.Name</td>
        <td align="right">@line.Product.Price</td>
        <td align="right">@(line.Quantity * line.Product.Price)</td>
        <td align="right">@Html.ActionLink("Remove", "RemoveItem", new { productId = line.Product.ProductID }, null)</td>
      </tr>
      index++;
    }
  </tbody>
  <tfoot></tfoot>
</table>

<input type="submit" value="Update Cart" />
}
@model CartTest.Models.Cart
@{
ViewBag.Title=“Index”;
}
购物车索引
量
项目
价格
小计
@{int index=0;}
@使用(Html.BeginForm(“UpdateCart”、“Cart”))
{
foreach(模型行中的var行)
{
@隐藏(“Lines.Index”,Index)
@文本框(“行[”+索引+“].Quantity”,行.Quantity)
@line.Product.Name
@行、产品、价格
@(行.数量*行.产品.价格)
@ActionLink(“Remove”,“removietem”,new{productId=line.Product.productId},null)
索引++;
}
}
移动

@{int index = 0;}
@using (Html.BeginForm("UpdateCart","Cart"))
{
将部分放在桌子上方,使整个桌子位于表单标签内。

移动

@{int index = 0;}
@using (Html.BeginForm("UpdateCart","Cart"))
{

将部分放在表格上方,使整个表格位于表单标记内。

它在
上方工作的原因是因为您在开口
内声明了BeginForm。它们必须正确嵌套才能工作。如果不想将“输入”按钮放在表格内,请将“输入”按钮移到表格元素外,使左大括号和右大括号处于同一级别

    @using (Html.BeginForm("UpdateCart","Cart"))
    {

    <table width="80%" align="center">

    <thead><tr>
    <th align="center">Quantity</th>
    <th align="left">Item</th>
    <th align="right">Price</th>
    <th align="right">Subtotal</th>
    </tr></thead>

    <tbody>

    @{int index = 0;}

    foreach (var line in Model.Lines)
    {
    <tr>
    @Html.Hidden("Lines.Index", index)
    <td align="center">@Html.TextBox("Lines[" + index + "].Quantity", line.Quantity)</td>
    <td align="left">@line.Product.Name</td>
    <td align="right">@line.Product.Price</td>
    <td align="right">@(line.Quantity * line.Product.Price)</td>
    <td align="right">@Html.ActionLink("Remove", "RemoveItem", new { productId = line.Product.ProductID }, null)</td>

    </tr>
        index++;

} 
    </tbody>
    <tfoot>
    </tfoot>
    </table>

    <input type="submit" value="Update Cart" />
    }
@使用(Html.BeginForm(“UpdateCart”、“Cart”))
{
量
项目
价格
小计
@{int index=0;}
foreach(模型行中的var行)
{
@隐藏(“Lines.Index”,Index)
@文本框(“行[”+索引+“].Quantity”,行.Quantity)
@line.Product.Name
@行、产品、价格
@(行.数量*行.产品.价格)
@ActionLink(“Remove”,“removietem”,new{productId=line.Product.productId},null)
索引++;
} 
}

它在
上方工作的原因是您在开口
内声明了BeginForm。它们必须正确嵌套才能工作。如果不想将“输入”按钮放在表格内,请将“输入”按钮移到表格元素外,使左大括号和右大括号处于同一级别

    @using (Html.BeginForm("UpdateCart","Cart"))
    {

    <table width="80%" align="center">

    <thead><tr>
    <th align="center">Quantity</th>
    <th align="left">Item</th>
    <th align="right">Price</th>
    <th align="right">Subtotal</th>
    </tr></thead>

    <tbody>

    @{int index = 0;}

    foreach (var line in Model.Lines)
    {
    <tr>
    @Html.Hidden("Lines.Index", index)
    <td align="center">@Html.TextBox("Lines[" + index + "].Quantity", line.Quantity)</td>
    <td align="left">@line.Product.Name</td>
    <td align="right">@line.Product.Price</td>
    <td align="right">@(line.Quantity * line.Product.Price)</td>
    <td align="right">@Html.ActionLink("Remove", "RemoveItem", new { productId = line.Product.ProductID }, null)</td>

    </tr>
        index++;

} 
    </tbody>
    <tfoot>
    </tfoot>
    </table>

    <input type="submit" value="Update Cart" />
    }
@使用(Html.BeginForm(“UpdateCart”、“Cart”))
{
量
项目
价格
小计
@{int index=0;}
foreach(模型行中的var行)
{
@隐藏(“Lines.Index”,Index)
@文本框(“行[”+索引+“].Quantity”,行.Quantity)
@line.Product.Name
@行、产品、价格
@(行.数量*行.产品.价格)
@ActionLink(“Remove”,“removietem”,new{productId=line.Product.productId},null)
索引++;
} 
}