登录控制器重定向不显示页面

时间:2014-04-01 09:17:16

标签: c# asp.net asp.net-mvc asp.net-mvc-3

我有登录控制器和视图。每当我点击登录按钮时,我都会检查给出的密码是否正确,并使用Redirect To Action重定向到我的主页。但是,每当我到达该页面时,页面都没有显示。我做错了什么???

在萤火虫中显示

GET http://localhost:64703/Home Page

应该是POST吗?

使用Javascript:

$('#Login').on('click', function () {
               var model = {};
                model["model.Password"] = $('#Password').val();
                    $.ajax({
                        url: '@Url.Action("Login", "LogIn")',
                        type: "POST",
                        data: model,
                        success: function (response) {
                        },
                        error: function (response) {
                        }
                    });
            });

查看:

<div >
        <div >
                @Html.ValidationSummary(true)

                <table style="margin-top: 10px;">
                    <tr>
                        <td >
                            @Html.LabelFor(model => model.Password)
                        </td>
                        <td >
                            @Html.PasswordFor(model => model.Password)
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <label id="errorMsg" class="calibri">
                            </label>
                        </td>
                    </tr>
                </table>
                <input type="button" id="Login" value="Login" />
        </div>
    </div>

控制器:

   [HttpPost]
            public ActionResult Login(LogIn model)
            {
    try
                {
                    EntityConnection connection = connect.getConnection();
                    Entities ctx = new Models.Entities(connection);                 
                    connection.Open();
                    connection.Close();
                    return RedirectToAction("Index", "HomePage");

                }
                catch (Exception ex)
                {                   
                    return Json(ex);
                }
}

这是我的代码..请让我知道应该做什么和不应该做什么。提前谢谢。

3 个答案:

答案 0 :(得分:0)

根据您发布的内容,我认为不存在匹配的ControllerAction

  • 控制器:主页
  • 行动:指数

请参阅RedirectToAction方法的说明。

答案 1 :(得分:0)

您正在使用ajax发出登录请求。

因此,您需要更新ajax响应处理程序以允许页面重定向。

        $('#Login').on('click', function () {
           var model = {};
            model["model.Password"] = $('#Password').val();
                $.ajax({
                    url: '@Url.Action("Login", "LogIn")',
                    type: "POST",
                    data: model,
                    success: function (response) {
                         // Check for login is successful
                         if (response.LoginResult == true) window.location = response.Redirect;
                         else alert("Login failed.");
                    },
                    error: function (response) {
                    }
                });
        });

//控制器

[HttpPost]
public ActionResult Login(LogIn model)
{
    try
    {
        EntityConnection connection = connect.getConnection();
        Entities ctx = new Models.Entities(connection);
        connection.Open();
        connection.Close();
        if (true) // Logic to validate login
        {
            return Json(new { LoginResult = true, Redirect = RedirectToAction("Index", "HomePage") });
        }

        return Json(new { LoginResult = false });
    }
    catch (Exception ex)
    {
        return Json(ex);
    }
}

以上示例模糊了登录逻辑 - 因此请使用您当前拥有的任何逻辑。

核心概念是控制器应返回客户端可以评估的json对象并确定要执行的操作。请注意,即使您的登录逻辑不正确,也会触发成功函数 - 只要服务器没有错误。

答案 2 :(得分:0)

仅更改您的jquery代码,重定向页面:

success: function (response) {
   alert("if this test dialog appears, so page will redirect");

   //just add this line:
   window.location.href = '@Url.Action("Index", "HomePage")';

},
相关问题