Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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
在asp.net mvc 5中使用Ajax登录表单_Asp.net_Ajax_Asp.net Mvc - Fatal编程技术网

在asp.net mvc 5中使用Ajax登录表单

在asp.net mvc 5中使用Ajax登录表单,asp.net,ajax,asp.net-mvc,Asp.net,Ajax,Asp.net Mvc,我正在使用ASP.NET MVC 5和ASP.NET标识进行身份验证。 我有一个登录表单的局部视图(这是一个模式),我想在不刷新页面的情况下登录用户(使用Ajax),但我不知道如何在Asp.NETMVC5中使用Ajax。 这是我在:Views/Shared/Partials/\u LoginModal.cshtml @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMetho

我正在使用ASP.NET MVC 5和ASP.NET标识进行身份验证。 我有一个登录表单的局部视图(这是一个模式),我想在不刷新页面的情况下登录用户(使用Ajax),但我不知道如何在Asp.NETMVC5中使用Ajax。 这是我在:
Views/Shared/Partials/\u LoginModal.cshtml

@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "", role = "form" }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        <div class="control-group">

            @Html.LabelFor(m => m.UserName, new { @class = "control-label hidden shown-ie8", @for = "inputEmail" })
            <div class="controls">

                @Html.TextBoxFor(m => m.UserName, new { @class = "input-block-level", @id = "inputEmail", @placeholder = "User Name" })
            </div>
        </div>
        <div class="control-group">
            <label class="control-label hidden shown-ie8" for="inputPassword">Password</label>
            @Html.LabelFor(m => m.Password, new { @class = "control-label hidden shown-ie8", @for = "inputPassword" })
            <div class="controls">

                @Html.PasswordFor(m => m.Password, new { @class = "input-block-level", @id = "inputPassword", @placeholder = "Password" })
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <label class="checkbox">
                    @Html.CheckBoxFor(m => m.RememberMe)
                    @Html.LabelFor(m => m.RememberMe)
                </label>
            </div>
        </div>
        <button type="submit" class="btn btn-primary input-block-level bold higher">
            Login
        </button>
    }
@使用(Html.BeginForm(“登录”,“帐户”,新的{ReturnUrl=ViewBag.ReturnUrl},FormMethod.Post,新的{@class=”,role=“form”}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@LabelFor(m=>m.UserName,新的{@class=“control label hidden show-ie8”,@for=“inputEmail”})
@Html.TextBoxFor(m=>m.UserName,新的{@class=“输入块级别”、@id=“inputEmail”、@placeholder=“用户名”})
暗语
@LabelFor(m=>m.Password,新的{@class=“control label hidden show-ie8”,@for=“inputPassword”})
@Html.PasswordFor(m=>m.Password,新的{@class=“input block level”,@id=“inputPassword”,@placeholder=“Password”})
@CheckBoxFor(m=>m.RememberMe)
@LabelFor(m=>m.RememberMe)
登录
}

我想要的另一件事是:当用户键入错误的用户名/密码时,我不想重定向到
/Account/LogIn
我想在我的登录表单中显示错误消息。

包括提交按钮的id:

<button type="submit" id="submitButton" ... >

使用jquery ajax发送数据:

<script>
    $(document).ready(function () {
        $("#submitButton").click(function (event) {
            $.ajax(
            {
                url: $("form").attr("action"),
                data: $("form").serialize(),
                dataType: "json",
                success: function (data) {
                    if (data == 'your error code or message')
                        alert('Wrong login/password');
                }
            });

            // Avoid to post 2 times.
            event.preventDefault();
        });
    });
</script>

$(文档).ready(函数(){
$(“#提交按钮”)。单击(函数(事件){
$.ajax(
{
url:$(“表单”).attr(“操作”),
数据:$(“表单”).serialize(),
数据类型:“json”,
成功:功能(数据){
如果(数据=='您的错误代码或消息')
警报(“错误登录/密码”);
}
});
//避免贴2次。
event.preventDefault();
});
});

谢谢兄弟,但是@Ajax.BeginingForm是什么?url:$(“form”).attr(“action”)我应该用action替换我的action名称吗?