C# 将数据从视图传递到控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/435387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:17:00  来源:igfitidea点击:

Passing data from View to Controller

c#.netasp.net-mvcviewcontroller

提问by Pompair

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 应用程序中,我正在为 Admin 制定逻辑以接受或拒绝新成员。我正在显示成员列表和两个按钮接受和拒绝,如下所示:

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

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

采纳答案by Marc Gravell

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,而不是 2*n 表单,或者使用 jQuery(或其他一些脚本助手)之类的东西来提交数据而无需表单(如果脚本可用)。

回答by tvanfosson

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),管理员可以为每个要批准的成员选择一个类似的一个用于拒绝,每个批准/拒绝按钮为整个表单选择一个。

回答by Pompair

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”字符串,但 Person 参数为 null。

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

干杯,庞