C# 如何将表单值传递给 .NET MVC 中的控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/667998/
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 12:35:35  来源:igfitidea点击:

How to pass Form Values to the Controller in .NET MVC

c#asp.net-mvcrequest

提问by

In ASP.net MVC:

在 ASP.NET MVC 中:

How should/Can I pass Form data (From the View) to the Controller?

我应该/如何将表单数据(从视图)传递给控制器​​?

This is the way I am heading :

这是我前进的方向:

  • The Controller Index function is passing a ViewModel object to the View.
  • The ViewModel object contains a paginated list as well as some SelectLists. _ The ViewModel object also contains a custom class I named theFilter. This class' purpose is to hold the Filter information Posted from the View via a Form.
  • I want the Index [AcceptVerbs(HttpVerbs.Post)] function to receive theFilter object populated with the form data, as well as the page number (as is it right now)
  • 控制器索引函数将一个 ViewModel 对象传递给 View。
  • ViewModel 对象包含一个分页列表以及一些 SelectList。_ ViewModel 对象还包含一个我命名为 theFilter 的自定义类。这个类的目的是保存通过表单从视图发布的过滤器信息。
  • 我希望索引 [AcceptVerbs(HttpVerbs.Post)] 函数接收填充了表单数据的过滤器对象,以及页码(现在是这样)

Here are snippets of my code:

以下是我的代码片段:

The Controller/Index postback function:

控制器/索引回发功能:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(int? page, CaseFilter caseFilter)
    {
        const int pageSize = 10;
        var cases = caseRepo.FindAllCases();
        var paginatedCases = new PaginatedList<Case>(cases, page ?? 0, pageSize);
        return View(new CaseIndexViewModel(paginatedCases, caseFilter));
    }

The Filter Class:

过滤器类:

public class CaseFilter
{
    int iVolume_id = 0,
        iSubject_id = 0;

    public CaseFilter() {

    }

    public int Volume_id { get { return iVolume_id; } set { iVolume_id = value; } }
    public int Subject_id { get { return iSubject_id; } set { iSubject_id = value; } }

}

And the ViewModel class:

和 ViewModel 类:

    public class CaseIndexViewModel
    {
    public PaginatedList<Case> PaginatedCases { get; private set; }
    public CaseFilter CaseFilter { get; private set; }

    public CaseIndexViewModel(PaginatedList<Case> paginatedCases, CaseFilter caseFilter)
    {

       PaginatedCases = paginatedCases;
       CaseFilter = caseFilter;
    }
}

Basically I am trying to avoid using Request.Form to populate the Filter class, at least not to use it within the Controller.

基本上我试图避免使用 Request.Form 来填充 Filter 类,至少不在控制器中使用它。

Any help, suggestions or disses are welcome!

欢迎任何帮助、建议或批评!

采纳答案by Morph

Finally, I do not need to even use the Request Collection. The CaseFilter object is filled automatically as I set it as a parameter in

最后,我什至不需要使用请求集合。CaseFilter 对象在我将其设置为参数时自动填充

public ActionResult Index(int? page, CaseFilter caseFilter)

The code above works as it is.

上面的代码按原样工作。

回答by BFree

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(FormCollection collection)
    {
         string valueFromNameTextBox = collection["name"];
    }

You can index into this collection with the names of all the inputs on the form.

您可以使用表单上所有输入的名称对该集合进行索引。

回答by tvanfosson

Rather than complicate my method signatures, I've taken to using the ValueProvider property and Try/UpdateModel in the Controller to retrieve form/route values unless the values are simple properties. On the other hand, I would probably also not make the filter part of the model for the View -- I tend to have a narrower conception of the model for the page, wanting it rather to be the businessmodel rather that a model of all the data on the page -- and would simply pass the filter values via ViewData.

我没有使我的方法签名复杂化,而是在控制器中使用 ValueProvider 属性和 Try/UpdateModel 来检索表单/路由值,除非这些值是简单的属性。另一方面,我可能也不会将过滤器作为视图模型的一部分——我倾向于对页面模型有一个更窄的概念,希望它是业务模型而不是所有模型页面上的数据 - 并且只需通过 ViewData 传递过滤器值。

回答by Morph

To expand BFree's answer, you can go through all the elements in the form by doing something like this:

要扩展 BFree 的答案,您可以通过执行以下操作来浏览表单中的所有元素:

foreach (string key in collection.keys) {
   if (key.contains("blah"))
      text1 = collection[key];
}

If it has too many elements for the key.contains if, it can get a bit ugly though, so beware ;).

如果 key.contains if 的元素太多,它可能会变得有点难看,所以要小心 ;)。