2016-10-14 11 views
0

私はフォームを使ってデータを入力する単純なMVCアプリケーションを作成しています。フォームを送信した後、特定の条件がfalseの場合は別のページを表示します。 条件がfalseの場合はReturnToAction("Form");を実行しても問題なく動作しますが、条件が成立した場合はRedirectToAction("MyPage");フォーム提出に関連するアクションが実行されていてもフォームが表示されます。 私は何が欠けていますか?あなたが同じフォームを表示したい場合はasp.net mvc redirecttoactionは別のページを表示していません

<%@ 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>

+0

コードブロックを置くことができますか、それは何が起こっているかを視覚化することはできますか? –

+2

は返されますRedirectToAction – Steve

答えて

0

は、あなたがRedirectResultを返すべきではありません。ありがとう これはあなたがここに私のForm.aspx私のコントローラ

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"); 

     } 

    } 

です。 Viewメソッドに電話する必要があります。また、フォーム提出を処理するhttp postアクションメソッド内で条件チェックを行う必要があります。

[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(); 
} 

someConditionCodeGoesHereを条件式のC#コードに置き換えます。ステートレスHTTPを受け入れるようにしてください。 httpコールの間に状態を維持するための静的変数は素晴らしい考えではありません。

ビューモデルを使用して、ビューとアクションメソッド間でデータを転送することも検討してください。見てくださいASP.Net MVC How to pass data from view to controller

+0

こんにちは@Shyju、私はあなたが言ったようにしようとしていますし、いくつかのブレークポイントをメソッド 'CheckConditions()'の中に入れました。ブレークポイントは正しくヒットしましたが、 'return RedirectToAction(" Success ");に達すると、ブラウザではまだ成功の代わりにフォームが表示されます。 – rosekarma

0

私は問題を解決しました。フォームの提出にエラーがあり、いつも失敗してしまいました...私はこの問題を解決し、RedirectToActionが働いた。

関連する問題