2

我目前有这个代码:

foreach (var item in Model.LstBagels)
            {

                    @Html.Hidden("txtId", item.BagelId)
                    <div id="bagel">                     
                        <div id="bagel-info">                  
                            <div id="bagel-image">
                                <img src="@Url.Content(String.Format("~/Resources/Images/{0}.jpg", item.Image))" alt="" />
                            </div> 

                            @if (@BestelBagels.Resources.Views.Index.IndexStrings.Name == "Name")
                            {
                                <h2>@Html.DisplayFor(m => item.Name)<br /></h2>
                            }
                            else
                            {
                                <h2>@Html.DisplayFor(m => item.NameFr)<br /></h2>
                            }

<input class="button widthorder" type="submit" name="BtnOrder" value="@BestelBagels.Resources.Views.Index.IndexStrings.Bestellen"/>
                        </div>   
                    </div>

当按下我的按钮时,我总是从所有项目或所有项目的列表中接收第一个 ID。但是我怎样才能收到我提交的 ID?我的 txtId 始终是我的 itemsource 中的第一个 Id。

问候

4

1 回答 1

3

按钮在哪里?

为什么不使用Form HtmlHelpers扭曲表单中的每个 div ?

你可以查看这篇文章

foreach (var item in Model.LstBagels)
            {
 @using (Html.BeginForm('ActionName', 'ContollerName', FormMethod.Post) {
                    @Html.Hidden("txtId", item.BagelId)
                    <div id="bagel">                     
                        <div id="bagel-info">                  
                            <div id="bagel-image">
                                <img src="@Url.Content(String.Format("~/Resources/Images/{0}.jpg", item.Image))" alt="" />
                            </div> 

                            @if (@BestelBagels.Resources.Views.Index.IndexStrings.Name == "Name")
                            {
                                <h2>@Html.DisplayFor(m => item.Name)<br /></h2>
                            }
                            else
                            {
                                <h2>@Html.DisplayFor(m => item.NameFr)<br /></h2>
                            }
<input class="button widthorder" type="submit" name="BtnOrder" value="@BestelBagels.Resources.Views.Index.IndexStrings.Bestellen"/>
                        </div> 

}

如果您不太关心非 js 浏览器并且您的操作接受 GET,您可以跳过表单使用并简单地构建您的操作按钮:

<input class="button widthorder" type="button" name="BtnOrder" onClick="window.location='@Url.Action("ActionName","ControllerName"   , new { txtId= item.BagelId })'" value="@BestelBagels.Resources.Views.Index.IndexStrings.Bestellen"/>
于 2013-11-04T11:17:31.107 回答