[英]C# - How to pass html input value to url.Action
我有一个input
和一个button
。 输入的值应该传递给@url.Action
就像下面代码中的描述:
<input class="in-class" id="textbox" type="text" runat="server" />
<button class="btn-class" id="press" type="button" onclick="location.href='@Url.Action("Index", "Home", new {id = /*Value Of textbox*/ })'" >click here</button>
正如我在代码中提到的, /*Value Of textbox*/
应该是输入的当前值。
使用 jQuery 或 JavaScript 更改 href 值,如下所示:
<button class="btn-class" id="press" type="button" onclick="changeHref()" >click here</button>
function changeHref(){
var url = '@Url.Action("Index", "Home")';
var txtVal = $('#textbox').val();
window.location.href = url + '/?txtVal=' + txtVal;
}
我用这个表格
<input type="text" id="txtValue" /> <input type="button" value="Detail" onclick="location.href='@Url.Action("Action", "Home")?Value=' + $('#txtValue').val()" />
或者你不能在 jquery 函数中写这个。
我更喜欢使用 jQuery click
处理程序,因为您有按钮 ID 并遵循标准事件注册模型来分隔 HTML 和 JS :
HTML
<input class="in-class" id="textbox" type="text" />
<button class="btn-class" id="press" type="button">click here</button>
JS
$('#press').click(function () {
var url = '@Url.Action("Index", "Home")';
var textValue = $('#textbox').val();
window.location.href = url + '?id=' + textValue;
});
PS:无需在 MVC 中使用runat="server"
属性,因为 Razor 不需要它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.