如何:.NET MVC2 按钮数组

发布于 2024-10-01 01:28:24 字数 1789 浏览 5 评论 0原文

我的视图控制器中有这样的内容

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMasterPage.Master"
    Inherits="System.Web.Mvc.ViewPage<MVC.ViewModels.EditViewModel>" %>
 <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <%
        Html.EnableClientValidation();%>
    <%
        using (Html.BeginForm("Edit", "Edit", FormMethod.Post, new { @class = "myform" }))
        {%>
        <%for (int i = 0; i < Model.Activity.Count; ++i)
                            {%>
        <div class="editor-field">
        <%:Html.DropDownListFor(x => x.Activity[i].SelectedActivity, Model.SelectListActivity)%>
         <input class="button" type="submit" value="<%:i%>" name="add_Button[<%:i%>]"/>
        </div>

    <%}
}
    %>
</asp:Content>

 [HttpPost, Authorize]
        public ActionResult Edit(string[] add_Button,  FormCollection collection)

当我转到控制器时,仅捕获索引 [0] 上的按钮,这是为什么? 两者都是ICollection; add_Button 和 string[] add_Button 对我不起作用。

换句话说,你如何发送这样的按钮数组:

   <input class="button" type="submit" value="Add" name="add_Button[0]" id="Submit3" />
   <input class="button" type="submit" value="Add" name="add_Button[1]" id="Submit4" />
   <input class="button" type="submit" value="Add" name="add_Button[2]" id="Submit5" />
   <input class="button" type="submit" value="Add" name="add_Button[3]" id="Submit6" />
   <input class="button" type="submit" value="Add" name="add_Button[4]" id="Submit7" />
   <input class="button" type="submit" value="Add" name="add_Button[5]" id="Submit8" />

谢谢你的帮助

I have something like this in my View

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMasterPage.Master"
    Inherits="System.Web.Mvc.ViewPage<MVC.ViewModels.EditViewModel>" %>
 <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <%
        Html.EnableClientValidation();%>
    <%
        using (Html.BeginForm("Edit", "Edit", FormMethod.Post, new { @class = "myform" }))
        {%>
        <%for (int i = 0; i < Model.Activity.Count; ++i)
                            {%>
        <div class="editor-field">
        <%:Html.DropDownListFor(x => x.Activity[i].SelectedActivity, Model.SelectListActivity)%>
         <input class="button" type="submit" value="<%:i%>" name="add_Button[<%:i%>]"/>
        </div>

    <%}
}
    %>
</asp:Content>

Controller:

 [HttpPost, Authorize]
        public ActionResult Edit(string[] add_Button,  FormCollection collection)

When I go to my controller, only the button on index [0] is captured, why is that?
both ICollection<string> add_Button and string[] add_Button did not work for me.

In another word, How do you send array of button like this:

   <input class="button" type="submit" value="Add" name="add_Button[0]" id="Submit3" />
   <input class="button" type="submit" value="Add" name="add_Button[1]" id="Submit4" />
   <input class="button" type="submit" value="Add" name="add_Button[2]" id="Submit5" />
   <input class="button" type="submit" value="Add" name="add_Button[3]" id="Submit6" />
   <input class="button" type="submit" value="Add" name="add_Button[4]" id="Submit7" />
   <input class="button" type="submit" value="Add" name="add_Button[5]" id="Submit8" />

Thank you for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女七分熟 2024-10-08 01:28:24

首先,您需要将该 DIV 设为 FORM 字段来包含您的输入。

其次,要增加 i 的值,您需要将其设置为 i++ 而不是 ++i。

最后,如果您只想重复按钮,则需要将 for 循环移动到按钮周围,而不是 div 和下拉列表周围。

<% for (int i= 0; i < Model.Activity.Count; i++)
{ %>
<input class="button" type="submit" value="Add" name="button[<%: i.ToString() %>]" id="Submit<%: i.ToString() %>" />
<% } %>

希望这有帮助!

Firstly you need to make that DIV a FORM field to contain your inputs.

Secondly, to increment the value of i you need to make it say i++ instead of ++i.

Lastly, if you only want the buttons to repeat, you need to move the for loop around the button, not around the div and dropdownlist.

<% for (int i= 0; i < Model.Activity.Count; i++)
{ %>
<input class="button" type="submit" value="Add" name="button[<%: i.ToString() %>]" id="Submit<%: i.ToString() %>" />
<% } %>

Hope this helps!

热情消退 2024-10-08 01:28:24

好吧......我发现了一些有趣的事情。
我的控制器
public ActionResult Edit(String[] add_Button, FormCollection collection)

除第一个按钮之外,单击的任何按钮都会转到 FormCollection。

因此,如果我单击 add_Button[1],它实际上会转到 collection["add_Button[1]"]

Ok....I found something interesting.
My controller
public ActionResult Edit(String[] add_Button, FormCollection collection)

Any button that is clicked other than first one goes to FormCollection.

So If I click add_Button[1], it will actually go to collection["add_Button[1]"]

紙鸢 2024-10-08 01:28:24

正如所承诺的,这是我的回答以及您更新的来源。

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMasterPage.Master"
    Inherits="System.Web.Mvc.ViewPage<MVC.ViewModels.EditViewModel>" %>
 <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <%
        Html.EnableClientValidation();%>

        <% for (int i = 0; i < Model.Activity.Count; i++)
        {%>

        <% using (Html.BeginForm("Edit", "Edit", FormMethod.Post, new { @class = "myform" }))
        {%>
        <div class="editor-field">
        <%:Html.DropDownListFor(x => x.Activity[i].SelectedActivity, Model.SelectListActivity)%>
         <input class="button" type="submit" value="<%: i.ToString() %>" name="add_Button[<%: i.ToString() %>]"/>
        </div>

    <%}
}
    %>
</asp:Content>

As promised, here is my answer with your updated source.

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMasterPage.Master"
    Inherits="System.Web.Mvc.ViewPage<MVC.ViewModels.EditViewModel>" %>
 <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <%
        Html.EnableClientValidation();%>

        <% for (int i = 0; i < Model.Activity.Count; i++)
        {%>

        <% using (Html.BeginForm("Edit", "Edit", FormMethod.Post, new { @class = "myform" }))
        {%>
        <div class="editor-field">
        <%:Html.DropDownListFor(x => x.Activity[i].SelectedActivity, Model.SelectListActivity)%>
         <input class="button" type="submit" value="<%: i.ToString() %>" name="add_Button[<%: i.ToString() %>]"/>
        </div>

    <%}
}
    %>
</asp:Content>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文