使用MVC时,如何调用控制器操作并传递文本框值?
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过将代码放入 Html.BeginForm("Retrieve", "Twitter") 块中,呈现给浏览器的 html 将包含在表单标签内,如下所示:
然后,当您单击提交按钮时,表单,以及文本框中的所有值都将发布到您的 MVC 应用程序。然后,MVC 负责将这些表单值(使用 @Html.TextBox("ConsumerSecretKey") 创建的文本框及其值)映射到控制器操作的参数。
在您的情况下,大致以下内容将呈现给浏览器(操作链接需要更改为“提交”类型的输入,正如我在下面所做的那样:
当将其发布回您的应用程序时,文本您在文本框中输入的内容(呈现为标签)将映射到与其名称匹配的操作方法的参数:
这里起作用的概念称为模型绑定。请参阅 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:
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:
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:
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
我使用了带有提交按钮的 Html.BeginForm,然后奇怪的是,文本框值会自动提交到服务器:
在我的控制器操作中:
I used, Html.BeginForm with a submit button, then oddly the textbox values are submitted to the server automatically:
And in my Controller Action:
您可以使用 FormCollection 对象。
You can use FormCollection object.
实际上,您无法使用
Html.ActionLink
进行 POST。您可以使用Ajax.ActionLink
并设置AjaxOptions
属性来实现此目的。如果使用form
不适合您,您始终可以实现 jQuery 函数来拦截由Html.ActionLink
生成的点击(基于此帮助程序生成的 ID)并添加文本框中的值作为参数。这里的问题是,如果您不使用 Ajax,您将使用 GET 方法提交值,这是一个很大的问题。 GET 应用于检索值,而不是修改数据库或其他后端数据存储的内容。如果你打算使用 Ajax,你可以这样做:In reality you cannot POST using
Html.ActionLink
. You can achieve that usingAjax.ActionLink
and settingAjaxOptions
properties. If usingform
is not an option for you, you can always implement jQuery function that intercepts a click generated byHtml.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: