ASP.net应用程序| Ajax调用没有得到控制器

时间:2017-06-05 07:06:20

标签: jquery sql asp.net ajax model-view-controller

我正在尝试使用ajax调用创建一个登录表单,但我从未进入控制器!

我得到[Object object],如下所示:Error

在我的控制器中,我有验证,如果盒子是空的或错误的,但我从来没有达到过。

控制器:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(User User)
        {


            string message = "";
            if (ModelState.IsValid)
            {
                if (a.CheckUserLogin(User) > 0)
                {
                    message = "success";
                }
                else
                {
                    message = "Uno de los campos es incorrecto";
                }
            }
            else
            {
                message = "Todos los campos son necesarios";
            }
            if (Request.IsAjaxRequest())
            {
                return Json(message, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

视图中的脚本:

 <script>
        $(document).ready(function () {
            $("#myModal").modal({
                backdrop: 'static',
            });
        });


        $("#Login").click(function () {

            var dataObject = { Username: $("#Username").val(), Password: $("#Password").val() };
            $.ajax({
                url: "../Login/Login",
                type: "POST",
                data: dataObject,
                datatype: "json",
                success: function (result) {
                    if (result.toString() == "success") {
                        alert(result);
                        $("#myModal").modal('hide');
                    }
                    else {
                        alert(result);
                    }
                },
                error: function (result) {
                    alert(result);
                    console.log('Error', result);
                }

            });

        });
    </script>

课程:

 public int CheckUserLogin(User user)
        {

            string connection = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;

            using(SqlConnection con = new SqlConnection(connection))
            {
                SqlCommand comObj = new SqlCommand("spLogin", con);
                comObj.CommandType = CommandType.StoredProcedure;
                comObj.Parameters.Add(new SqlParameter("@username", user.Username));
                comObj.Parameters.Add(new SqlParameter("@password", user.Password));
                con.Open();
                return Convert.ToInt32(comObj.ExecuteScalar());
            }



        }

这是应该触发它的形式,我认为问题是没有进入控制器。

   @using(Html.BeginForm()){ 
          @Html.AntiForgeryToken()
    <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <form class="form-horizontal">
                    <div class="modal-header">
                        <div id="messenger">

                        </div>
                    </div>

                    <br />

                    <div class="form-group">
                        <label for="Username" class="col-lg-3 control-label">User Name:</label>
                        <input class="form-control" id="Username" placeholder="User Name" type="text" />
                    </div>

                    <div class="form-group">
                        <label for="Password" class="col-lg-3 control-label">Password:</label>
                        <input class="form-control" id="Password" placeholder="Password" type="password" />
                    </div>
                    <div class="modal-footer">
                        <a href="#" class="btn btn-primary pull-left marg" id="Login">Login</a>

                    </div>

                </form>

            </div>

        </div>
    </div>
    }

0 个答案:

没有答案