2017-05-08 59 views
0

我無法弄清楚我做錯了什麼。 我的項目啓動了一個索引視圖,發佈到fileUpload操作。 從fileUpload動作中,控制器中的另一個方法稱爲changeText(),在changeText()結尾處調用帶有參數字符串的Results(文本)。在Results()的最後,有一行返回View()到Results.cshtml。但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狀態碼的空響應主體。就通過網絡瀏覽器訪問您的網站的用戶而言,這意味着一個完全空白的頁面,這不是您想要的。

你想在這裏,返回ViewResult利用ResultX.cshtml。這意味着您的FileUpload操作需要返回ViewResult或更一般地ActionResultActionResult是所有其他的結果類型的基類,因此,如果您鍵入您的回報方式,你可以返回任何樣的結果你喜歡,不管是ViewResultRedirectResultEmptyResult

鑑於此,存在因爲方法是多餘的,所以changeTextResultX。沒有理由在三種不同的方法之間傳遞這些信息,這幾乎都是同樣的事情。請直接在FileUpload中進行return View("ResultX", txt);。但是,一個成功的帖子應該遵循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(); 
} 
相關問題