开发者

How to: .NET MVC2 array of buttons

开发者 https://www.devze.com 2023-01-23 13:10 出处:网络
I have something like this in my View <%@ Page Title=\"\" Language=\"C#\" MasterPageFile=\"~/Views/Shared/ViewMasterPage.Master\"

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开发者_C百科 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


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!


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.

<%@ 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消