2011-07-12 14 views
0

最初に使用したとき、私はそれを気に入って、Webフォーム<%:%>などビュー・エンジン のように混乱しました。 「{」かっこが置かれている場所や他のシナリオに過敏であることに気付くことはできません。これは、古いビューエンジンが厄介ではない点でエラーを出します。カート・インデックス・ビュー(Razorビュー・エンジン)のRazor構文に関する助けが必要

たとえば、フォームヘルパーの閉じ括弧} が</table>タグの下にあるため、次のコードではエラーが発生します。私がそれを</tbody>の上に置くとうまくいきます!しかし、サブミットボタン入力がネストされていなければならず、ボタン入力をテーブルに入れたくないので、 は必要ありません。

@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" /> 
} 

答えて

3

</tbody>より上で動作する理由は、開始番号<tbody>の中にBeginFormが宣言されているためです。彼らは正しく動作するためにはネストされていなければなりません。入力ボタンをテーブルの内側に配置したくない場合は、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" /> 
    } 
0
テーブル全体はformタグの内側にあるので、あなたのテーブルの上に

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

部分を上に移動

関連する問題