从 mvc 2 视图中的复选框返回数据
我在 MVC2 项目的视图中使用复选框来允许用户选择多个对象。这是我认为的代码(跳过不相关的行:
<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: <%= Model.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(); %> (我删除了开始表单行周围的 <%%>
,以便显示我的整个帖子) 一切都完美地显示在视图中。我从用户那里收到所有其他数据。我只是不知道如何从选定的复选框接收数据
[HttpPost]
public ActionResult CreateRequest1(int NewBuildings, int NewFloors, int NewOffices, string comments, int[] Description)
我应该在此处添加什么来获取选定的值?
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: <%= Model.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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要向您的方法添加一个参数,例如:
[http邮报]
public ActionResult CreateRequest1(int NewBuildings, int NewFloors, int NewOffices, string comments, ICollection Description)
我假设您的值是 int,但您可以根据需要更改它。
您可以在这里阅读更多内容:
http://haacked.com/archive/2008 /10/23/model-binding-to-a-list.aspx
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