如何在 asp.net mvc3 中使用 @html.actionlink 传递文本值?
这里的所有答案都不起作用。已接受的答案不能像普通操作链接一样刷新页面;其余答案根本不起作用,或要求您放弃原来的问题并停止使用 ActionLink
。
MVC3/4
您可以使用ActionLink
方法的htmlAttributes
来实现您想要的功能:
Html.ActionLink("My Link Title", "MyAction", "MyController", null, new { onclick = "this.href += '&myRouteValueName=' + document.getElementById('myHtmlInputElementId').value;" })
MVC5
以下错误已被报告,但我尚未验证:
检测到可能存在危险的 Request.Path 值
@using (Html.BeginForm(actionName,controllerName)) {
<input type="text" name="myText"/>
<input type="submit" value="Check your value!">
}
string myText = Request.Form["myText"].Value;
- vapcguy不要使用 @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;
});
});
你可以使用这段代码(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);
}
}
<httpRuntime requestPathInvalidCharacters=",*,%,:,\,?" />
- guytz72