如何:.NET MVC2 按钮数组
我的视图控制器中有这样的内容
<%@ 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
对我不起作用。
换句话说,你如何发送这样的按钮数组:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您需要将该 DIV 设为 FORM 字段来包含您的输入。
其次,要增加 i 的值,您需要将其设置为 i++ 而不是 ++i。
最后,如果您只想重复按钮,则需要将 for 循环移动到按钮周围,而不是 div 和下拉列表周围。
希望这有帮助!
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.
Hope this helps!
好吧......我发现了一些有趣的事情。
我的控制器
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]"]
正如所承诺的,这是我的回答以及您更新的来源。
As promised, here is my answer with your updated source.