【问题标题】:ASP.NET MVC Passing form values to action methodASP.NET MVC 将表单值传递给操作方法
【发布时间】:2012-09-04 12:35:23
【问题描述】:

我有以下表格

<form name="SearchForm" method="post" id="SearchForm" action="/Search/">

还有下面的按钮

<input type="button" onclick="javascript:document.SearchForm.submit();" class="btn-leftsearch">

点击此按钮,表单提交并调用此方法

[HttpPost]
public ActionResult Index(string querystring)
{
   return View();
}

当然查询字符串为空。我想将查询字符串或最好是代表表单中字段的其他内容传递给控制器​​。我尝试过在表单标签中使用 action 属性。我试图将数据添加到按钮中的 onclick 方法中。没有任何工作。我想做的就是传递一些这样的数据

Search?pri=all&amenity=pool etc

在控制器中我会有类似的东西

[HttpPost]
public ActionResult Index(string pri, List<string> amenities)
{
   ...
}

谁能告诉我如何将这些数据传递给视图?

【问题讨论】:

    标签: asp.net-mvc forms


    【解决方案1】:

    我想建议您可以使用以下代码片段来解决您的问题。

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

    关于收藏请在搜索文本框中输入名称。您将获得实际输入的值。 您可以使用表单上所有输入的名称对该集合进行索引。

    【讨论】:

    • 我明白了,但我还有其他一些我想传递的数据,这些数据不在表单字段中。我如何通过这些?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    相关资源
    最近更新 更多