将带有Ajax的表单发布到ASP.NET MVC控制器
当我点击链接“Foo”时,我在登录操作中点击了断点,但用户名总是空的。我不明白为什么将带有Ajax的表单发布到ASP.NET MVC控制器,.net,ajax,asp.net-mvc,asp.net-mvc-4,.net,Ajax,Asp.net Mvc,Asp.net Mvc 4,当我点击链接“Foo”时,我在登录操作中点击了断点,但用户名总是空的。我不明白为什么 <a class="jqselect" id="myId" href="#">Foo</a> @using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { id = "fooForm" })) { <input type="text" id="username"/> } <scrip
<a class="jqselect" id="myId" href="#">Foo</a>
@using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { id = "fooForm" }))
{
<input type="text" id="username"/>
}
<script type="text/javascript">
$(document).ready(function () {
$(".jqselect").click(function () {
$.ajax({
url: '/Account/LogOn',
type: "Post",
data: $('#fooForm').serialize(),
success: function (result) {
if (result.success) {
}
}
});
});
});
</script>
[HttpPost]
public ActionResult LogOn(string username)
{
Console.WriteLine(username);
return new EmptyResult();
}
@使用(Html.BeginForm(“LogOn”、“Account”、FormMethod.Post、new{id=“fooForm”}))
{
}
$(文档).ready(函数(){
$(“.jqselect”)。单击(函数(){
$.ajax({
url:“/Account/LogOn”,
类型:“Post”,
数据:$('#fooForm')。序列化(),
成功:功能(结果){
如果(结果、成功){
}
}
});
});
});
[HttpPost]
公共操作结果登录(字符串用户名)
{
Console.WriteLine(用户名);
返回新的EmptyResult();
}
您必须给它起个名字:
<input type="text" id="username" name="username"/>
其中,您的模型定义为:
public class LogOnModel {
public string Username {get;set;}
}
[HttpPost]
public ActionResult LogOn(LogOnModel input)
{
}
您的方法定义为:
public class LogOnModel {
public string Username {get;set;}
}
[HttpPost]
public ActionResult LogOn(LogOnModel input)
{
}
你必须给它起个名字:
<input type="text" id="username" name="username"/>
其中,您的模型定义为:
public class LogOnModel {
public string Username {get;set;}
}
[HttpPost]
public ActionResult LogOn(LogOnModel input)
{
}
您的方法定义为:
public class LogOnModel {
public string Username {get;set;}
}
[HttpPost]
public ActionResult LogOn(LogOnModel input)
{
}