[英]asp.net mvc redirecttoaction does not shows another page
我正在編寫一個簡單的MVC應用程序,在其中使用表單來放置一些數據,提交表單后,如果特定條件為false
我想再次顯示該表單,否則將顯示另一頁。 當條件為假時,我執行ReturnToAction("Form");
並且它可以完美工作,但是當條件為true時,我執行RedirectToAction("MyPage");
即使執行了與表單提交有關的操作,我仍然看到該表單。 我想念什么? 謝謝,這是我的控制器
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");
}
}
這是我的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>
如果要顯示相同的表單,則不應返回RedirectResult
。 您應該調用View
方法。 另外,您還應該在處理表單提交的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();
}
用條件條件表達式的C#代碼替換someConditionCodeGoesHere
。 嘗試使用無狀態Http。 在http調用之間保持狀態的靜態變量不是一個好主意。
還可以考慮使用視圖模型在視圖和操作方法之間傳輸數據。 看一下ASP.Net MVC如何將數據從視圖傳遞到控制器
我解決了這個問題。 表單提交中存在錯誤,總是在失敗時出現……我解決了這個問題, RedirectToAction
起作用了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.