I'm using checkboxes in the view of my MVC2 project to allow users to select multiple objects. Here is the code in my view(skipping unrelated lines:
<h2>Install New Equipment</h2>
//Html.BeginForm("CreateRequest1", "Home", FormMethod.Post);
<div>Employee's First Name: <%= Model.Employee.EmpFName%></div>
<div>Employee's Last Name: <%= M开发者_JAVA百科odel.Employee.EmpLName%></div>
<div>Employee's Phone Number: <%= Model.Employee.Phone%> </div>
<br />
<div>Please select the equipment you would like to request:</div><br />
<div> <% foreach (var info in ViewData.Model.EquipDescription)
{ %>
<% = Html.CheckBox("Description", info.ID) %><%=info.Description%> <br />
<%} %>
</div><br />
<div>Please Select the Location for the Equipment to be Installed </div><br />
<div>Building <%= Html.DropDownList("NewBuildings", new SelectList((IEnumerable)ViewData["buildings"], "ID", "Buildings")) %>
Floor <%= Html.DropDownList("NewFloors", new SelectList((IEnumerable)ViewData["floors"], "ID", "FloorNumber")) %>
Office<%= Html.DropDownList("NewOffices", new SelectList((IEnumerable)ViewData["offices"], "ID", "OfficeNumber")) %>
</div>
<br />
<div>Comments: <%=Html.TextArea("Comments") %></div><br />
<%Html.EndForm(); %>
(I removed the <%%>
around the begin form line so my whole post would show)
Everything display perfectly in the view. I recieve all the other data from the user. I just don't know how to recieve the data from the selected checkboxes
[HttpPost]
public ActionResult CreateRequest1(int NewBuildings, int NewFloors, int NewOffices, string comments, int[] Description)
What should I add here to get the selected values?
You'll need to add a parameter to your method like: [HttpPost] public ActionResult CreateRequest1(int NewBuildings, int NewFloors, int NewOffices, string comments, ICollection Description)
I assume your value is an int, but you can change it if required.
You can read more here: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
精彩评论