asp.net-mvc 如何在asp.net mvc 3 中使用@html.actionlink 传递文本框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11293048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Pass textbox value using @html.actionlink in asp.net mvc 3
提问by kiransh
How to Pass value from text using @html.actionlink in asp.net mvc3 ?
如何在asp.net mvc3 中使用@html.actionlink 从文本传递值?
采纳答案by Suraj Shrestha
Rather than passing your value using @Html.actionlink, try jquery to pass your textbox value to the controller as:
与其使用 @Html.actionlink 传递您的值,不如尝试使用 jquery 将您的文本框值传递给控制器:
$(function () {
$('form').submit(function () {
$.ajax({
url: this.action,
type: this.method,
data: { search: $('#textboxid').val()},
success: function (result) {
$('#mydiv').html(result);
}
});
return false;
});
});
This code will post your textbox value to the controller and returns the output result which will be loaded in the div "mydiv".
此代码会将您的文本框值发布到控制器并返回将加载到 div“mydiv”中的输出结果。
回答by toddmo
None of the answers here really work. The accepted answer doesn't refresh the page as a normal action link would; the rest simply don't work at all or want you to abandon your question as stated and quit using ActionLink.
这里没有一个答案真的有效。接受的答案不会像正常操作链接那样刷新页面;其余的根本不起作用,或者希望您按照说明放弃您的问题并停止使用ActionLink.
MVC3/4
MVC3/4
You can use the htmlAttributesof the ActionLinkmethod to do what you want:
您可以使用htmlAttributesofActionLink方法来执行您想要的操作:
Html.ActionLink("My Link Title", "MyAction", "MyController", null, new { onclick = "this.href += '&myRouteValueName=' + document.getElementById('myHtmlInputElementId').value;" })
MVC5
MVC5
The following error has been reported, but I have not verified it:
报了以下错误,但我没有验证:
A potentially dangerous Request.Path value was detected
检测到潜在危险的 Request.Path 值
回答by Rzv.im
to pass data from the client to the server you could use a html form:
要将数据从客户端传递到服务器,您可以使用 html 表单:
@using (Html.BeginForm(actionName,controllerName)) {
<input type="text" name="myText"/>
<input type="submit" value="Check your value!">
}
be sure to catch your myText variable inside your controller's method
确保在控制器的方法中捕获 myText 变量
回答by Aghilas Yakoub
you can use this code (YourValue = TextBox.Text)
您可以使用此代码 (YourValue = TextBox.Text)
Html.ActionLink("Test", new { controller = "YourController", action = "YourAction", new { id = YourValue }, null );
public class YourController : Controller
{
public ActionResult YourAction(int id)
{
return View("here your value", id);
}
}

