Asp.net mvc 使用MVC时,如何调用控制器操作并传递文本框值?

Asp.net mvc 使用MVC时,如何调用控制器操作并传递文本框值?,asp.net-mvc,controller,html.actionlink,Asp.net Mvc,Controller,Html.actionlink,如何在使用Html.ActionLink时从文本框中读取值,以便将该值传递给操作 我有以下代码: <table> <tr> <th> Consumer Key: </th> <td> @Html.TextBox("ConsumerKey") </td>

如何在使用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>

用户密钥:
@Html.TextBox(“ConsumerKey”)
使用者密钥:
@Html.TextBox(“ConsumerCretKey”)
@ActionLink(“检索访问令牌”,“/Retrieve”
基本上,我需要调用控制器操作并传递文本框值

MVC怎么可能做到这一点


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

我使用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>
    }

您可以使用FormCollection对象

[HTTPPost]
 public ActionResult Retrieve(FormCollection collection)
    {
         string consumerKey=collection["ConsumerKey"];
         string consumerSecretKey=collection["ConsumerSecretKey"];
    }
通过在表单中使用提交按钮来使用表单发布方法


通过将代码放在Html.BeginForm(“Retrieve”、“Twitter”)块中,呈现给浏览器的Html将包含在一个表单标记中,类似于:

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


这里使用的概念称为模型绑定。请参阅并向下滚动到“Action Method Parameters”概述部分

实际上,您不能使用
Html.ActionLink
发布。您可以使用
Ajax.ActionLink
和设置
AjaxOptions
属性来实现这一点。如果您不选择使用
表单
,您可以始终实现jQuery函数,拦截
Html.ActionLi生成的点击nk
(基于此助手生成的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);
        });
    });
});

这就是MVC的工作方式。表单/浏览器提交发送到服务器的HTTP POST中的所有输入。MVC将根据各种绑定规则自动将输入绑定到操作参数。如果您遵循ASP.NET MVC开发指南,此功能将扩展到模型的简单类型之外。您应该提供[HttpPost]属性
<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
}
$(document).ready(function() {
    $("myActionLinkId").click(function() {
        var textBoxValue = $("#myTextBoxId").val();

        $.post($(this).attr("href"), { id: textBoxValue }, function(result) {
            alert("Result data: " + result);
        });
    });
});