简体   繁体   English

RedirectToAction 在控制器中工作但不更新视图和 URL

[英]RedirectToAction works in controller but does not update the view and URL

I have a controller for Login/Register and related actions.我有一个用于登录/注册和相关操作的控制器。 Ever since I changed the view from the one I made with Html helpers to test, to the one that my UI designer provided with html, after login I'm not redirected to the index page.自从我将视图从我用 Html 助手制作的视图更改为我的 UI 设计师提供的 html 后,登录后我没有重定向到索引页面。

At first I thought that the login form was not submitting but after inserting some Console debug codes I realized that in the controller I am being redirected to the Index action but the Index view is not being showed in my browser and the URL is not changing.起初我认为登录表单没有提交,但在插入一些控制台调试代码后,我意识到在控制器中我被重定向到索引操作,但索引视图没有显示在我的浏览器中并且 URL 没有改变。 Some developers with the same problem said it was because of the AJAX buttons but I don't think my view has those as far as I know and I don't know how to make sure.一些有同样问题的开发人员说这是因为 AJAX 按钮,但我认为我的视图没有我所知道的那些,我不知道如何确定。

This is my AccountController Code and the actions in it.这是我的 AccountController 代码和其中的操作。 I removed redundant codes from it:我从中删除了多余的代码:

public ActionResult Index()
{
    Console.WriteLine("Redirected To Index");
    return View(model);
}
public ActionResult Login()
{            
    Console.WriteLine("Entered Login Get Action");
    return View();
}

[HttpPost]
public ActionResult Login(LoginViewModel model)
{
    Console.WriteLine("Entered Login Post Action");
    if (ModelState.IsValid)
    {
        Console.WriteLine("Login Redirect Action");
        return RedirectToAction("Index");
    }
    return View(model);
}

and this is the form in my Login View that is being submitted with redundant codes removed:这是我的登录视图中的表单,正在提交并删除冗余代码:

     <form class="cozy" method="post">
        @Html.TextBoxFor(model => model.Username)
        @Html.PasswordFor(model => model.Password)
        <input type="submit" name="login" value="Login"/>
     </form>

After submitting, "Redirected To Index" is printed in the console along with the previous lines, but I don't get redirected to the actual page.提交后,“重定向到索引”与前几行一起打印在控制台中,但我没有被重定向到实际页面。

Thanks in advance for spending time to answer to my problem.提前感谢您花时间回答我的问题。

It appears the problem was with some js scripts in the view, preventing the redirect.看来问题出在视图中的一些 js 脚本上,阻止了重定向。 I removed them and the code started working properly again.我删除了它们,代码又开始正常工作了。 then I added those js references again and now the code works.然后我再次添加了那些 js 引用,现在代码可以工作了。 I don't know why and how it didn't before but this was what I did that made it work again.我不知道为什么和以前没有,但这是我所做的使它再次起作用。

I think that you missed action attribute on the form.我认为您错过了表单上的action属性。

<form class="cozy" method="post" action="@Url.Action("Login","Account")">
        @Html.TextBoxFor(model => model.Username)
        @Html.PasswordFor(model => model.Password)
        <input type="submit" name="login" value="Login"/>
</form>

That will call Login action with post method when you hit submit button on your form.当您点击表单上的提交按钮时,这将使用 post 方法调用Login操作。 َAs suggested in comments, You can use Html.BeginForm razor helper instead of pure html code. َ正如评论中所建议的,您可以使用Html.BeginForm razor helper 而不是纯 html 代码。

@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { id = "myform", @class = "cozy", enctype = "multipart/form-data" })) {
        @Html.TextBoxFor(model => model.Username)
        @Html.PasswordFor(model => model.Password)
        <input type="submit" name="login" value="Login"/>
}

Instead of using return View(model);而不是使用return View(model); try to provide the full path of the view:尝试提供视图的完整路径:

public ActionResult Index()
{
    Console.WriteLine("Redirected To Index");
    return View("add actual path of the view", model); 
    //For Example
    //return View("~/Views/Test/Login.cshtml", model); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM