【问题标题】:When using MVC, how to call a Controller Action and Pass Text Box values?使用 MVC 时,如何调用控制器动作并传递文本框值?
【发布时间】:2012-02-10 10:43:52
【问题描述】:

使用 Html.ActionLink 时如何从文本框中读取值,以便将值传递给操作?

我有以下代码:

<table>
        <tr>
            <th>
                Consumer Key:
            </th>
            <td>
                @Html.TextBox("ConsumerKey")
            </td>
        </tr>
        <tr>
            <th>
                Consumer Secret Key:
            </th>
            <td>@Html.TextBox("ConsumerSecretKey")
            </td>
        </tr>
        <tr>
        <th>
        </th>
        <td>@Html.ActionLink("Retreive Access Tokens", "/Retrieve"</td>
        </tr>
    </table>

基本上,我需要调用控制器操作并传递文本框值。

用 MVC 怎么可能?

我知道我可以只使用一个 html 按钮和对该 Action 的 AJAX 调用来实现它,但我希望有另一种使用 MVC 控件的方法。

【问题讨论】:

    标签: asp.net-mvc controller html.actionlink


    【解决方案1】:

    通过将您的代码放入 Html.BeginForm("Retrieve", "Twitter") 块中,呈现给浏览器的 html 将包含在表单标签中,类似于:

    <form method="POST" action="/Retrieve/Twitter">
        <table>...</table>
    </form>
    

    然后,当您单击提交按钮时,表单以及文本框中的所有值都将发布到您的 MVC 应用程序。然后 MVC 将这些表单值(使用 @Html.TextBox("ConsumerSecretKey") 创建的文本框及其值)映射到控制器操作的参数。

    在您的情况下,大致以下内容将呈现给浏览器(操作链接需要更改为“提交”类型的输入,如下所示:

    <form method="POST" action="/Retrieve/Twitter">
        <table>
            <tr>
                <th>
                    Consumer Key:
                </th>
            <td>
                <input id="ConsumerKey" name="ConsumerKey" type="text" value="" />
            </td>
        </tr>
        <tr>
            <th>
                Consumer Secret Key:
            </th>
            <td>
                <input id="ConsumerSecretKey" name="ConsumerSecretKey" type="text" value="" />
            </td>
        </tr>
    
        <td><input type="submit" id="Retrieve" value="Retreive Access Tokens" /></td>
    
        </tr>
    
    </table>
    
    </form>
    


    当这被发回您的应用程序时,您在文本框中输入的文本(呈现为标签)将映射到与其名称匹配的操作方法的参数:

    public ActionResult Retrieve(string consumerKey, string consumerSecretKey)
    {
        //action method code here
    }
    


    这里起作用的概念称为模型绑定。请参阅Controllers and Action Methods in ASP.NET MVC Applications 并向下滚动到“操作方法参数”部分以了解概览

    【讨论】:

      【解决方案2】:

      我用的是带提交按钮的 Html.BeginForm,然后奇怪的是文本框的值会自动提交到服务器:

      @using (Html.BeginForm("Retrieve", "Twitter"))
          {
          <table>
              <tr>
                  <th>
                      Consumer Key:
                  </th>
                  <td>
                      @Html.TextBox("ConsumerKey")
                  </td>
              </tr>
              <tr>
                  <th>
                      Consumer Secret Key:
                  </th>
                  <td>@Html.TextBox("ConsumerSecretKey")
                  </td>
              </tr>
              <tr>
                  <th>
                  </th>
                  <td>
                      <input type="submit" id="Retrieve" value="Retreive Access Tokens" />
                  </td>
              </tr>
          </table>
          }
      

      在我的控制器动作中:

       public ActionResult Retrieve(string consumerKey, string consumerSecretKey)
              {
      }
      

      【讨论】:

      • 这就是 MVC 的工作原理。表单/浏览器在发送到服务器的 HTTP POST 中提交所有输入。 MVC 会根据各种绑定规则自动将输入绑定到动作参数。如果您遵循 ASP.NET MVC 开发指南,此功能会从简单类型扩展到您的模型。
      【解决方案3】:

      您可以使用 FormCollection 对象。

      [HTTPPost]
       public ActionResult Retrieve(FormCollection collection)
          {
               string consumerKey=collection["ConsumerKey"];
               string consumerSecretKey=collection["ConsumerSecretKey"];
          }
      

      通过在表单中​​使用提交按钮来使用表单发布方法。

      【讨论】:

      • 你应该给 [HttpPost] 属性
      【解决方案4】:

      实际上你不能使用Html.ActionLink POST。您可以使用Ajax.ActionLink 并设置AjaxOptions 属性来实现。如果您不适合使用form,您始终可以实现 jQuery 函数,该函数拦截Html.ActionLink 生成的点击(基于此帮助程序生成的 ID)并从文本框中添加一个值作为参数。这里的问题是,如果您不使用 Ajax,您将使用 GET 方法提交值,这是一个很大的问题。 GET 应该用于检索值,而不是用于修改数据库或其他后端数据存储的内容。如果你打算使用 Ajax,你可以这样做:

      $(document).ready(function() {
          $("myActionLinkId").click(function() {
              var textBoxValue = $("#myTextBoxId").val();
      
              $.post($(this).attr("href"), { id: textBoxValue }, function(result) {
                  alert("Result data: " + result);
              });
          });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多