2015-07-13 50 views
0

there; 我的MVC項目中有一個非常奇怪的Tempdata問題。這是我的僞代碼;MVC TempData issue

public class MyController: Controller 
    { 
    public ActionResult CreateInvoiceAndCustomerContact() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult CreateCustomerContact_Invoice() 
    { 
     { 
      _MyFileCreationObj.CreateTtextFile(); 
     } 
     TempData["ResultMessage"] = "hello"; 
     return RedirectToAction("CreateInvoiceAndCustomerContact"); 
    } 
} 

In object _MyFileCreationObj I have a method which uses "StreamWriter" to create text file: 

    public void CreateInvoiceAndCustomerContact() 
    { 
     using (StreamWriter writer = new StreamWriter(exportedFile)) 
     { 
      //write text to a file 
     } 
    } 

我遇到的問題是: 的 「TempData的[」 ResultMessage 「]」 不會在我的視圖中顯示。

如果我註釋了StreamWriter塊,那麼在我的視圖中顯示「TempData [」ResultMessage「]」沒有問題。

有人請請幫助嗎?

乾杯 Rob。

+0

我也試圖與代碼另一個測試:公共的ActionResult TestTempData() { 使用(System.IO.StreamWriter作家=新System.IO.StreamWriter(@「d:\文件夾\某物.txt「)) { writer.WriteLine(」sth !!!「); } TempData [「Message」] =「TestMsg」; return RedirectToAction(「LoadInvoiceAndCustomerContact」); }此消息不能顯示在視圖上。如果我刪除了「使用」塊,那麼沒有問題顯示消息。有人可以對此做出解釋嗎?切斯,羅伯特 – user1427235

回答