在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个回答


改为尝试</ p>

  return redirect(“ / Home / Index”)
</ code> </ pre>
     </ div>

展开原文

原文

try this instead

return redirect("/Home/Index")


您可能会在此处滥用Ajax功能</ p>

您应该使用 @ Html.ActionLink(“ Login”,“ LoginAccess”,“ Login”)</ strong> </ p>

Ajax最初用于从服务器端获取某些信息,而不影响当前正在浏览的页面。 </ p>
     </ div>

展开原文

原文

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.


在执行 Ajax </ code>调用时,不能强制从控制器重定向。 您可以通过2种方式解决此问题:</ p>


  1. 用常规get替换ajax调用。</ li>
  2. 从操作中返回json并使用来自javascript的重定向</ li>
    </ ol>
         </ div>

展开原文

原文

When you are doing Ajax calls, you cannot force redirect from controller. You can fix this by 2 ways:

  1. Replace ajax call with regular get.
  2. Return a json from the action and use redirect from javascript


您可以尝试</ p>

  var结果= new ControllerName()。YourAction();
</ code> </ pre>
     </ div>

展开原文

原文

You can try this

var result = new ControllerName().YourAction();

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐