asp.net-mvc – 使用MVC时,如何调用Controller Action和Pass Tex
发布时间:2020-12-16 09:50:42 所属栏目:asp.Net 来源:网络整理
导读:如何在使用 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时从文本框中读取值,以便将值传递给操作?
我有以下代码: <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> 基本上,我需要调用Controller Action并传递文本框值. 如何使用MVC实现这一目标? 我知道我可以使用一个html按钮和对该Action的AJAX调用来实现它,但我希望有另一种方法可以使用MVC控件. 解决方法
通过将代码放在Html.BeginForm(“Retrieve”,“Twitter”)块中,呈现给浏览器的html将封装在form-tag中,如:
<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并向下滚动到“操作方法参数”部分 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET是否从bin中将所有程序集加载到AppDomain中?
- asp.net-mvc – 使用Automapper将字符串映射到枚举
- 在ASP.NET MVC 6中继续支持Ninject?
- asp.net – 实体框架:坚持在多对多添加新实体,而不是重新使
- asp.net – 如何从usercontrol处理页面事件? (VB.net)
- asp.net – 关于MVC3中助手的清晰度?
- ASP.NET AJAX日历扩展程序日期格式
- asp.net-mvc-3 – 如何将数据自动完成HTML属性添加到TextBo
- asp.net-mvc – 我可以在MVC 3中有/多个_Layout页面吗?为c
- asp.net-mvc – 关于Ajax调用Register动作方法的书呆子晚餐
推荐文章
站长推荐
- asp.net – 如何在客户端上为CustomValidator控件
- asp.net-mvc – 在模型/视图/控制器键盘快捷键之
- 我应该如何组织我的ASP.Net主题和常见的CSS文件
- entity-framework – 有没有办法让ASP.NET 5依赖
- asp.net-mvc – Web API模型绑定器不能与HttpPos
- asp.net-mvc – 使用NHibernate,ASP.NET MVC和Dr
- asp.net-web-api – 交叉原始SignalR连接在协商后
- 如何使用ASP.Net集群上传文件夹?
- asp.net-mvc-2 – 覆盖“Html.EditorFor”上的类
- asp.net-mvc – 在ASP.Net MVC中寻找转发器类型功
热点阅读