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

发布于 2025-01-05 01:03:04 字数 824 浏览 1 评论 0原文

使用 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 控件的方法。

How to read from a text box value when using Html.ActionLink so that the value can be passed to the action?

I have the below code:

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

Basically, I'd need to call the Controller Action and pass the text box values.

How would that be possible with MVC?

I know I can implement it using just an html button and an AJAX call to that Action but I hoped there would be another way using MVC controls.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

追星践月 2025-01-12 01:03:04

通过将代码放入 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
}

这里起作用的概念称为模型绑定。请参阅 ASP.NET MVC 应用程序中的控制器和操作方法 并向下滚动到“操作方法参数”部分以获取概述

By putting your code inside the Html.BeginForm("Retrieve", "Twitter") block, the html that is rendered to the browser will be enclosed inside a form-tag, something like:

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

Then when you click on the submit button the form, along with all the values in the text boxes will be posted to you MVC application. Then MVC does the work of mapping these form values (the text boxes created using @Html.TextBox("ConsumerSecretKey") and their values) to the parameters of your controller actions.

In your case, roughly the following will be rendered to the browser (the action link will need to be changed to an input of type "submit," as I have done below:

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

When this is posted back to your Application the text you entered into the text boxes (rendered as tags) would map to the parameters of your action method matching their name:

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

The concept at work here is called model-binding. See Controllers and Action Methods in ASP.NET MVC Applications and scroll down to the "Action Method Parameters" section for an overview

归途 2025-01-12 01:03:04

我使用了带有提交按钮的 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)
        {
}

I used, Html.BeginForm with a submit button, then oddly the textbox values are submitted to the server automatically:

@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>
    }

And in my Controller Action:

 public ActionResult Retrieve(string consumerKey, string consumerSecretKey)
        {
}
看轻我的陪伴 2025-01-12 01:03:04

您可以使用 FormCollection 对象。

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

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

You can use FormCollection object.

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

Use form post method by using a submit button in your form.

月野兔 2025-01-12 01:03:04

实际上,您无法使用 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);
        });
    });
});

In reality you cannot POST using Html.ActionLink. You can achieve that using Ajax.ActionLink and setting AjaxOptions properties. If using form is not an option for you, you can always implement jQuery function that intercepts a click generated by Html.ActionLink (based on an ID generated by this helper) and add a value from text box as a parameter. The problem here is that if you don't use Ajax you will submit the values using GET method and this is a big NO. GET should be used for retrieving values, and not for modifying the contents of a database or other backend data store. If you plan to use Ajax you could do something like this:

$(document).ready(function() {
    $("myActionLinkId").click(function() {
        var textBoxValue = $("#myTextBoxId").val();

        $.post($(this).attr("href"), { id: textBoxValue }, function(result) {
            alert("Result data: " + result);
        });
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文