从 mvc 2 视图中的复选框返回数据

发布于 2024-11-01 23:22:31 字数 1723 浏览 0 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(1

旧情勿念 2024-11-08 23:22:31

您需要向您的方法添加一个参数,例如:
[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

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