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. When the condition is false, I do ReturnToAction("Form");
and it works perfectly, but, when the condition is true and I do 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:
<%@ 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
. you should call the View
method. Also you should do the condition checking inside the http post action method which handles the form submission.
[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. Try to embrace Stateless Http. Static variables to keep state between http call's are not a great idea.
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
I resolved the problem. There was an error in form submission, which went always in on failure... I resolved this problem and RedirectToAction
worked.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.