weixin_33724570 2013-08-19 09:13 采纳率: 0%
浏览 61

在ASP.Net MVC4中重定向

I'm using ASP.Net MVC4 Razor. I'm having a problem with redirection. I wanna redirect the user to the Home controller at the user login(if login is valid). But my problem is it always come back to the login page even the redirect meythod also fired.

Here is my code..

public class LoginController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult LoginAccess()
    {
        return RedirectToAction("Index", "Home");
    }

}

Login page..

<div class="body_wraper">
<div id="cloud" style="background:url('@Url.Content("~\\Content\\cloud.png")');">
    <div id="login_form">
        <div class="Three-Dee">Login here..</div>
         <table>
            <tbody>
                <tr>
                    <td>@Html.Label("Username")</td>
                    <td></td>
                    <td>@Html.TextBox("txtUsername")</td>
                </tr>
                <tr>
                    <td>@Html.Label("Password")</td>
                    <td></td>
                    <td>@Html.Password("txtPassword")</td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td style="text-align:right;"><button class="k-button" id="login" onclick="Login()">Login</button></td>
                </tr>
            </tbody>
         </table>
    </div>
</div>

<script type="text/javascript">

function Login() {
    $.ajax({
        url: '@Url.Content("~/Login/LoginAccess")',
        type: 'POST'
    });
}

Home Controller..

public ActionResult Index()
    {
        Session["UserName"] = "Administrator";
        string menu = this.GetMenu();
        ViewBag.ManueItems = menu;
        return View("User");
    }

After click on the login button it comes to LoginAccess in Login controller and then comes to Home controller Index method, but doesn't view the "user view". But when i check with typing url >>(host__/Login/LoginAccess">http://__host__/Login/LoginAccess) Its working properly. Please help me to slove this problem. Thank you.. :)

  • 写回答

4条回答

  • 10.24 2013-08-19 09:20
    关注

    You may misuse the Ajax function here

    You should use @Html.ActionLink("Login", "LoginAccess", "Login") instead

    Ajax is originally used to get something from server side other than affecting currently browsing page.

    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号