从View传递数据控制器 [英] Passing data from View to Controller

查看:125
本文介绍了从View传递数据控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC应用程序,我正在做的逻辑联系,以接受或拒绝新成员。我展示接受和拒绝,成员和两个按钮的列表是这样的:

In an ASP.NET MVC application, I'm making logic for Admin to accept or reject new members. I'm showing a list of members and two buttons Accept and Reject, like this:

<% foreach (var mm in (ViewData["pendingmembers"] as List<MyMember>)) %>
<% { %>
   <tr><td>Username:<%=mm.UserName %></td><td>
   <tr><td>Firstname:<%=mm.FirstName %></td><td>
   ...etc...
   <tr>
      <td>
      <% using (Html.BeginForm("AcceptPendingUser", "Admin"))
      { %>
          <input type="submit" value="Accept" />
      <% } %>
      </td>
      <td>
      <% using (Html.BeginForm("RejectPendingUser", "Admin"))
      { %>
        <input type="submit" value="Reject" />
      <% } %>
      </td>
    </tr>
<% } %>

所以,待处理部件的数据的列表在MyMember-对象的列表。每个MyMember对象将被打印出来成员和两个按钮都设置为管理员可以选择接受或拒绝挂起的成员。

So, the list of pending member data is in a list of MyMember-objects. Each MyMember object will be printed out member and two buttons are setup for the admin to either accept or reject a pending member.

然后,在我分开的这两个输入域/表格的处理,像这样的控制器:

Then, in the controller I'm separating the handling of those two input fields/forms, like this:

public ActionResult AcceptPendingUser()
{
   // TODO: Add code to save user into DB and send welcome email.
   return RedirectToAction("Index");
}

public ActionResult RejectPendingUser()
{
   // TODO: Add code to remove user from PendingUsers list and send rejection email.
   return RedirectToAction("Index");
}

我想直接获取对象旁边的按钮,用户pressed。 我怎样才能从视图到控制器发送MyMember对象? 或者,我怎么可能送一个数字指标与按钮preSS?也许有一个隐藏字段?

I would like to directly get the object next to the button the user pressed. How can I send the MyMember object from the View to the controller? Or how do I send perhaps a numeric index with button press? Maybe with a hidden field?

推荐答案

最简单的方法可能是一个隐藏输入:

The simplest option would probably be a hidden input:

<input type="hidden" value="<%=mm.Key%>" name="key" id="key" />

(名称相应地,在每个表单中)

(name accordingly; in each form)

这两个控制器将然后采取所谓的关键的论点(重命名,以适应)。如果要分析来自多个输入的对象,你需要一个 ModelBinder的。当然,而不是2 * N的形式,可以考虑两种查询字符串基于URL或使用类似的jQuery(或其他脚本助手)提交数据,而不需要的表格(如果脚本可用)。

The two controller would then take an argument called "key" (rename to suit). If you want to parse the object from multiple inputs, you'll need a ModelBinder. Of course, rather than 2*n forms, you might consider either query-string based urls, or use something like jQuery (or some other script helper) to submit the data without needing the forms (if script is available).

这篇关于从View传递数据控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆