C#-如何将html输入值传递给url.Action
我有一个C#-如何将html输入值传递给url.Action,c#,html,asp.net-mvc,razor,C#,Html,Asp.net Mvc,Razor,我有一个输入和一个按钮。输入的值应该传递给@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 tex
输入
和一个按钮
。输入的值应该传递给@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>
点击这里
正如我在代码中提到的,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;
}
点击这里
函数changeHref(){
var url='@url.Action(“Index”,“Home”);
var txtVal=$('#textbox').val();
window.location.href=url+'/?txtVal='+txtVal;
}
使用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;
}
点击这里
函数changeHref(){
var url='@url.Action(“Index”,“Home”);
var txtVal=$('#textbox').val();
window.location.href=url+'/?txtVal='+txtVal;
}
我使用此表单
或者你不能在jquery函数中写这个。我使用这个表单
或者您不能在jquery函数中写入此项。我更喜欢使用jquery单击处理程序,因为您有按钮ID和以下内容:
HTML
<input class="in-class" id="textbox" type="text" />
<button class="btn-class" id="press" type="button">click here</button>
PS:无需在MVC中使用runat=“server”
属性,因为Razor不需要它。我更喜欢使用jQuery单击
处理程序,因为您有按钮ID和以下内容:
HTML
<input class="in-class" id="textbox" type="text" />
<button class="btn-class" id="press" type="button">click here</button>
PS:无需在MVC中使用runat=“server”
属性,因为Razor不需要它。为此使用表单。您不能。你需要为此编写javascript。你可以发布控制器操作的代码吗?使用表单。你不能。您需要为此编写javascript。您可以发布控制器操作的代码吗?忘记了查询字符串。您必须在“/”之后添加?txtVal=。我更新了代码它是否在主控制器中命中索引方法?在方法中放置断点,查看是否返回到服务器。如果没有,则是url被设置。它们都不会命中断点,首先(“/”)它只打开txtVal作为链接,但当我使用“/?txtVal=”时,什么都不会发生。请尝试,window.location.href=url+”?id='+txtVal;使用post或get代替window.location然后:$.post(url,{txtVal:txtVal});忘记了查询字符串。您必须在“/”之后添加?txtVal=。我更新了代码它是否在主控制器中命中索引方法?在方法中放置断点,查看是否返回到服务器。如果没有,则是url被设置。它们都不会命中断点,首先(“/”)它只打开txtVal作为链接,但当我使用“/?txtVal=”时,什么都不会发生。请尝试,window.location.href=url+”?id='+txtVal;使用post或get代替window.location然后:$.post(url,{txtVal:txtVal});这应该行得通,但我个人会建议也加入一个函数。这应该行得通,但我个人会建议也加入一个函数。