【问题标题】:ASP MVC 3 Get hiddenfield value of specific item in foreachASP MVC 3获取foreach中特定项目的隐藏字段值
【发布时间】:2013-11-15 00:03:33
【问题描述】:

我目前有这个代码:

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。

问候

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3 http-post html-helper


    【解决方案1】:

    按钮在哪里?

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

    您可以查看this post:

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

    【讨论】:

    • 添加了我的按钮,我的错。扭曲表单中的每个 div 是什么意思?
    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 2013-03-09
    • 2014-04-10
    相关资源
    最近更新 更多