2017-05-08 7 views
0

私は何が間違っているのか分かりません。 私のプロジェクトは、fileUploadアクションに投稿するインデックスビューで起動します。 fileUploadアクションから、コントローラ内の別のメソッドはchangeText()と呼ばれ、changeText()の最後にはパラメータ文字列を取るResults(text)が呼び出されます。 Results()の最後には、Results.cshtmlへの戻り値View()があります。しかし、Results.cshtmlはロードされません。 私は何かが不足しているかどうかだけ考えています。.netの別のコントローラアクションからビューをロードする

 public ActionResult Index() 
     { 

      return View(); 
     } 

     [HttpPost] 
     public void FileUpload(HttpPostedFileBase file) 
     { 
      Debug.WriteLine(file.FileName) 
      if (file != null) 
      { 
       var fileName = Path.GetFileName(file.FileName); 
       pathName = Path.Combine(Server.MapPath("~/Content/Images"), fileName); 
       file.SaveAs(pathName); 
      } 

      changeText(txt); 

     } 


    public void changeText (string text) 
    { 

       ResultX(textChange);    
    } 



     public ActionResult ResultX(string text) 
     { 

       Debug.WriteLine("resultx action"); 
       return View(text); 

     } 

Thanks. 
+0

右のビューを呼び出していることを確認して動作していない場合。 – OmG

+0

ご使用のasp.net MVCの正しいバージョンのタグを更新してください。 – ps2goat

答えて

1

私はこれらすべての方法の理由を見ることはできません! あなたのコードは非常に簡単です。

public ActionResult Index() 
    { 

     return View(); 
    } 

    [HttpPost] 
    public ActionResult FileUpload(HttpPostedFileBase file) 
    { 
     Debug.WriteLine(file.FileName) 
     if (file != null) 
     { 
      var fileName = Path.GetFileName(file.FileName); 
      pathName = Path.Combine(Server.MapPath("~/Content/Images"), fileName); 
      file.SaveAs(pathName); 
     } 

     Debug.WriteLine("resultx action"); 
     return View("ResultX",text); 

    } 

このすべては、あなたがあなたのコントローラを共有

2

これはあなたのコードがどうあるべきかです:

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

[HttpPost] 
public ActionResult FileUpload(HttpPostedFileBase file) 
{ 
    Debug.WriteLine(file.FileName) 
    if (file != null) 
    { 
     var fileName = Path.GetFileName(file.FileName); 
     pathName = Path.Combine(Server.MapPath("~/Content/Images"), fileName); 
     file.SaveAs(pathName); 
    } 

    return changeText(txt); 
} 

public ActionResult changeText (string text) 
{ 
     return ResultX(textChange);    
} 

public ActionResult ResultX(string text) 
{ 
     return View("ResultX", text); 
} 
1

あなたが実際の行動がどのように動作するかを理解するようにそれはいないようです。まず、アクションの戻り値の型によって、クライアントに送信される応答が決まります。返信voidは、単純に200個のステータスコードを持つ空の応答本文を返すことを意味します。ユーザーがWebブラウザを介してサイトにアクセスするという点では、完全に空白のページになります。これはあなたが望むものではありません。

ここではViewResultを利用してResultX.cshtmlを返します。つまり、FileUploadアクションはViewResult以上、一般的にはActionResultを返す必要があります。 ActionResultはそれが存在する、ということを考えるとViewResultRedirectResultEmptyResultなど

あるかどうか、あなたのリターンそのように入力ので、もし、あなたが好きな結果のいずれかの種類を返すことができ、他のすべての結果タイプの基本クラスです方法としてのchangeTextおよびResultXは不必要である。 3つの異なる方法の間でこれを渡す理由は全くありません。すべて同じことをします。ちょうどreturn View("ResultX", txt);FileUploadに直接入れてください。しかし、成功した投稿はPRG(Post-Redirect-Get)パターンに従う必要があります。そのため、ユーザーがページを更新すると、フォームは再送信されません。結果として、は本当にになりました。正常にリダイレクトされ、元のFileUploadビューのエラーが返されます。全体として、それは次のようになります:

[HttpPost] 
public ActionResult FileUpload(HttpPostedFileBase file) 
{ 
    if (file != null) 
    { 
     // Moved this inside the conditional because you can't 
     // reference `file.FileName` unless `file` is non-null 
     Debug.WriteLine(file.FileName); 

     var fileName = Path.GetFileName(file.FileName); 
     pathName = Path.Combine(Server.MapPath("~/Content/Images"), fileName); 
     file.SaveAs(pathName); 
     return RedirectToAction("Index"); 
    } 

    // Traditionally, you'd pass in what was posted to the call to `View`, 
    // but you cannot repopulate a file input, so there's no point here. 
    return View(); 
} 
関連する問題