C# 单击按钮将参数传递给控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13822114/
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
Passing parameters to Controller on click of a button
提问by iJade
This might be a trivial question, me being a new bee :) Here is my View code
这可能是一个微不足道的问题,我是一只新蜜蜂:) 这是我的查看代码
<button type="button" id="btnLogin" onclick="location.href='<%: Url.Action("Authenticate", "Login") %>'" runat="server"></button>
Here is my Controller code
这是我的控制器代码
public ActionResult Authenticate(string username,string password)
{
bool status = Login.Authenticate(username, password);
return View("Status", status);
}
I'm able to go to that controller but i'm unable to pass the parameters.Can any let me know what is the rite way to pass parameters to controller function.
我可以去那个控制器,但我无法传递参数。任何人都可以让我知道将参数传递给控制器函数的仪式方式是什么。
Full View Code
完整查看代码
<table>
<tr>
<td>
<input type="text" id="txtUsername" runat="server" />
</td>
<td>
<input type="text" id="txtPassword" runat="server" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<button type="button" id="btnLogin" onclick="location.href='<%: Url.Action("Authenticate", "Login") %>'" runat="server"></button>
</td>
</tr>
</table>
回答by robasta
Use a <form>that posts to the Authenticate method in your controller. Also change the <button>to a <input type="submit"
使用<form>发布到控制器中的 Authenticate 方法的 。也将更<button>改为<input type="submit"
Like this:
像这样:
<form method="post" action="@Href("~/account/authenticate")" id="LogOnForm">
.... @*input boxes*@
<input type="submit" value="Login"/>
</form>
EDIT: for ajax login, use this script (jquery is required). Note that the form needs an ID
编辑:对于 ajax 登录,请使用此脚本(需要 jquery)。请注意,表单需要一个ID
<script type="text/javascript">
$(document).ready(function () {
$("#LogOnForm").submit(function (event) {
event.preventDefault();
$.ajax({
type: "POST",
url: '@Url.Content("~/account/authenticate")',
data: $("#LogOnForm").serialize(),
success: function (data) {
if (data.Status) {
//Logged in succesfully
} else {
//Login failed
}
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.responseText);
}
});
});
});
</script>
EDIT: Controller Modification:
编辑:控制器修改:
public JsonResult Authenticate(string username,string password)
{
bool status = Login.Authenticate(username, password);
return Json( new @"Status", status);
}
回答by AliR?za Ad?yah?i
You can pass input values with its nameattribute.
您可以通过其name属性传递输入值。
<%using (Html.BeginForm("Authenticate", "Login"))
{%>
<table>
<tr>
<td>
<input type="text" id="txtUsername" name="username" />
</td>
<td>
<input type="text" id="txtPassword" name="password" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Login" class="submit_button"/>
</td>
</tr>
</table>
<%}%>
CONTROLLER
控制器
[HttpPost]
public ActionResult Authenticate(string username, string password)
{
bool status = Login.Authenticate(username, password);
return View("Status", status);
}
回答by Chris Harland
You need to at the parameters into the call string using Javascript
您需要使用 Javascript 将参数添加到调用字符串中
<script type="text/javascript">
function postdata(location) {
var url = '<%: Url.Action("Authenticate", "Login", new { username = XXX, password = YYY) %>';
var Username = document.getElementById(Username).value;
var Password= document.getElementById(Password).value;
url = url.replace("XXX", Username );
url = url.replace("YYY", Password);
location.href=url;
}
</script>
<button type="button" id="btnLogin" onclick="postdata()" runat="server"></button>
回答by AjItH V S
Missing name property on your input fields. Add a name property to your username and password input boxes. The name property should be same as your action controller arguments.
输入字段上缺少 name 属性。将名称属性添加到您的用户名和密码输入框中。name 属性应该与您的操作控制器参数相同。
<table>
<tr>
<td>
<input type="text" id="txtUsername" runat="server" name="username" />
</td>
<td>
<input type="text" id="txtPassword" runat="server" name="password"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<button type="button" id="btnLogin" onclick="location.href='<%: Url.Action("Authenticate", "Login") %>'" runat="server"></button>
</td>
</tr>
</table>
回答by DarkVision
you know there is a third parameter to url action where you can add parameter
您知道 url 操作有第三个参数,您可以在其中添加参数
@Url.Action("Authenticate", "Login" , new {name = "name"})
you just need a way to add your value there
你只需要一种方法来增加你的价值

