简体   繁体   English

asp.net mvc redirecttoaction不显示其他页面

[英]asp.net mvc redirecttoaction does not shows another page

I am writing a simple MVC application in which I use a form to put some data, and after I submit the form, if a particular condition is false I would like to show the form again, otherwise I would show another page. 我正在编写一个简单的MVC应用程序,在其中使用表单来放置一些数据,提交表单后,如果特定条件为false我想再次显示该表单,否则将显示另一页。 When the condition is false, I do ReturnToAction("Form"); 当条件为假时,我执行ReturnToAction("Form"); and it works perfectly, but, when the condition is true and I do RedirectToAction("MyPage"); 并且它可以完美工作,但是当条件为true时,我执行RedirectToAction("MyPage"); I still see the form, even if the action related to the form submission has been executed. 即使执行了与表单提交有关的操作,我仍然看到该表单。 What am I missing? 我想念什么? Thank you This is my controller 谢谢,这是我的控制器

public class HomeController : Controller
{
    private static bool condition = false;

    public ActionResult Form()
    {
        return View();
    }

    [HttpPost]
    public ActionResult CheckCondition(FormCollection form)
    {

        string username = form["txtUsername"];

        condidion = true;            

        return RedirectToAction("MyPage");
    }

    public ActionResult MyPage()
    {

        if (!condition)
        {

            return RedirectToAction("Form");
        }
        else 
        {

            return RedirectToAction("MyPage");

        }

    }

Here you are my Form.aspx: 这是我的Form.aspx:

 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> <!DOCTYPE html> <html> <head runat="server"> <title>Form</title> </head> <body> <form runat="server" method="post"> <ext:ResourceManager runat="server" /> <ext:Viewport runat="server" Layout="CenterLayout"> <Items> <ext:FormPanel runat="server" id="Form" Url="/Home/CheckCondition" Width="500" Height="500" Layout="CenterLayout" > <Items> <ext:Window ID="Window1" runat="server" Closable="false" Resizable="false" Height="200" Icon="Lock" Title="Dashboard Login" Draggable="false" Width="350" Modal="true" BodyPadding="5" Layout="FormLayout" > <Items> <ext:TextField ID="txtUsername" runat="server" FieldLabel="Username" AllowBlank="false" BlankText="Your username is required." Text="Demo" /> <ext:TextField ID="txtPassword" runat="server" InputType="Password" FieldLabel="Password" AllowBlank="false" BlankText="Your password is required." Text="Demo" /> </Items> <Buttons> <ext:Button ID="btnLogin" runat="server" Text="Login" Icon="Accept"> <Listeners> <Click Handler=" if (!#{txtUsername}.validate() || !#{txtPassword}.validate()) { Ext.Msg.alert('Error','The Username and Password fields are both required'); // return false to prevent the btnLogin_Click Ajax Click event from firing. return false; } else { #{Form}.submit(); }" /> </Listeners> </ext:Button> </Buttons> </ext:Window> </Items> </ext:FormPanel> </Items> </ext:Viewport> </form> </body> </html> 

If you want to show the same form, you should not return a RedirectResult . 如果要显示相同的表单,则不应返回RedirectResult you should call the View method. 您应该调用View方法。 Also you should do the condition checking inside the http post action method which handles the form submission. 另外,您还应该在处理表单提交的http post action方法内部进行条件检查。

[HttpPost]
public ActionResult CheckCondition(FormCollection form)
{
    if (someConditionCodeGoesHere==false)  // replace this with your condition code
    {
        return View();
    }
    else 
    {
        return RedirectToAction("Success");
    }
}
public ActionResult Success()
{
   return View();
}

replace someConditionCodeGoesHere with your C# code for your condition expression. 用条件条件表达式的C#代码替换someConditionCodeGoesHere Try to embrace Stateless Http. 尝试使用无状态Http。 Static variables to keep state between http call's are not a great idea. 在http调用之间保持状态的静态变量不是一个好主意。

Also consider using a view model to transfer data between your view and action method. 还可以考虑使用视图模型在视图和操作方法之间传输数据。 Take a look at ASP.Net MVC How to pass data from view to controller 看一下ASP.Net MVC如何将数据从视图传递到控制器

I resolved the problem. 我解决了这个问题。 There was an error in form submission, which went always in on failure... I resolved this problem and RedirectToAction worked. 表单提交中存在错误,总是在失败时出现……我解决了这个问题, RedirectToAction起作用了。

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

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