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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题