Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将带有Ajax的表单发布到ASP.NET MVC控制器_.net_Ajax_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

将带有Ajax的表单发布到ASP.NET MVC控制器

将带有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

当我点击链接“Foo”时,我在登录操作中点击了断点,但用户名总是空的。我不明白为什么

<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)
{
}