将数据从View传递到Controller

时间:2021-11-07 12:39:57

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:

在ASP.NET MVC应用程序中,我为管理员制作了接受或拒绝新成员的逻辑。我正在显示一个成员列表和两个按钮Accept和Reject,如下所示:

<% 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>
<% } %>

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.

因此,待处理成员数据列表位于MyMember对象列表中。每个MyMember对象将打印出成员,并设置两个按钮供管理员接受或拒绝待定成员。

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");
}

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?

我想直接获取用户按下的按钮旁边的对象。如何将View中的MyMember对象发送到控制器?或者如何通过按下按钮发送数字索引?也许有一个隐藏的领域?

3 个解决方案

#1


The simplest option would probably be a hidden input:

最简单的选项可能是隐藏的输入:

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

(name accordingly; in each form)

(相应地命名;每种形式)

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).

然后两个控制器将采用一个名为“key”的参数(重命名以适应)。如果要从多个输入中解析对象,则需要一个ModelBinder。当然,您可以考虑使用基于查询字符串的URL,或者使用jQuery(或其他一些脚本帮助程序)之类的东西来提交数据而不需要表单(如果脚本可用),而不是2 * n表单。

#2


Instead of using an HTML button consider using an ActionLink and construct it to include the id of the member being approved. Another alternative would be to have a checkbox (whose value is the id of the member being approved) that the admin can select for each member to be approved and a similar one for reject and one each approve/reject buttons for the entire form.

不要使用HTML按钮,而是考虑使用ActionLink并将其构造为包含要批准的成员的ID。另一种选择是拥有一个复选框(其值是被批准的成员的ID),管理员可以为每个要批准的成员选择一个复选框,类似的一个用于拒绝,一个用于批准/拒绝整个表单的按钮。

#3


Answering to myself and other mvc newbies:

回答我自己和其他mvc新手:

I got it finally working with this code:

我终于使用了这段代码:

VIEW:

 <%=Html.ActionLink(
     "Jump", 
     "Jump", 
     new { name=(ViewData["Person"] as Person).Name, 
     person=ViewData["Person"]}, 
     null) %>

CONTROLLER:

public ActionResult Index()
{
     ViewData["Title"] = "Home Page";
     ViewData["Message"] = "Welcome to ASP.NET MVC!";
     Person p = new Person();
     p.Name = "Barrack";
     p.Age = 35;
     ViewData["Person"] = p;
     return View();
}

public ActionResult Jump(string name, Person person)
{
    return View();
}

Debugging the app in the Jump method gives me nice "Barrack"-string for the name parameter, but Person parameter in null.

在Jump方法中调试应用程序为name参数提供了很好的“Barrack”-string,但是null参数中的Person参数。

I also understand what the kind commenters tried to explain: it's easy to send simple data types like strings and ints to controller, but complex types such as my Person object needs something else.

我也理解那些评论者试图解释的内容:将简单的数据类型(如字符串和整数)发送到控制器很容易,但复杂的类型(如Person对象)需要其他东西。

Basically passing an int is enough for me. The hardest part here was figuring out the right way to set up ActionLink.

基本上传递一个int对我来说已经足够了。这里最难的部分是找出设置ActionLink的正确方法。

Cheers, Pom

#1


The simplest option would probably be a hidden input:

最简单的选项可能是隐藏的输入:

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

(name accordingly; in each form)

(相应地命名;每种形式)

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).

然后两个控制器将采用一个名为“key”的参数(重命名以适应)。如果要从多个输入中解析对象,则需要一个ModelBinder。当然,您可以考虑使用基于查询字符串的URL,或者使用jQuery(或其他一些脚本帮助程序)之类的东西来提交数据而不需要表单(如果脚本可用),而不是2 * n表单。

#2


Instead of using an HTML button consider using an ActionLink and construct it to include the id of the member being approved. Another alternative would be to have a checkbox (whose value is the id of the member being approved) that the admin can select for each member to be approved and a similar one for reject and one each approve/reject buttons for the entire form.

不要使用HTML按钮,而是考虑使用ActionLink并将其构造为包含要批准的成员的ID。另一种选择是拥有一个复选框(其值是被批准的成员的ID),管理员可以为每个要批准的成员选择一个复选框,类似的一个用于拒绝,一个用于批准/拒绝整个表单的按钮。

#3


Answering to myself and other mvc newbies:

回答我自己和其他mvc新手:

I got it finally working with this code:

我终于使用了这段代码:

VIEW:

 <%=Html.ActionLink(
     "Jump", 
     "Jump", 
     new { name=(ViewData["Person"] as Person).Name, 
     person=ViewData["Person"]}, 
     null) %>

CONTROLLER:

public ActionResult Index()
{
     ViewData["Title"] = "Home Page";
     ViewData["Message"] = "Welcome to ASP.NET MVC!";
     Person p = new Person();
     p.Name = "Barrack";
     p.Age = 35;
     ViewData["Person"] = p;
     return View();
}

public ActionResult Jump(string name, Person person)
{
    return View();
}

Debugging the app in the Jump method gives me nice "Barrack"-string for the name parameter, but Person parameter in null.

在Jump方法中调试应用程序为name参数提供了很好的“Barrack”-string,但是null参数中的Person参数。

I also understand what the kind commenters tried to explain: it's easy to send simple data types like strings and ints to controller, but complex types such as my Person object needs something else.

我也理解那些评论者试图解释的内容:将简单的数据类型(如字符串和整数)发送到控制器很容易,但复杂的类型(如Person对象)需要其他东西。

Basically passing an int is enough for me. The hardest part here was figuring out the right way to set up ActionLink.

基本上传递一个int对我来说已经足够了。这里最难的部分是找出设置ActionLink的正确方法。

Cheers, Pom