您可以將數據存儲在TempData
或Session
,叫RedirectToAction
,然後再從TempData
或Session
檢索值。
TempData
是特別的。它存儲Session中的內容,但通過TempData
存儲的數據僅保留用於當前請求和後續請求。之後,數據被拋出。這聽起來很適合你的需求,但如果你需要數據保持更長的時間,只需使用Session。
public class HomeController : Controller
{
public ActionResult Index()
{
var someData = TempData["SomeData"] as string; // can be anything, using a string as an example;
return View(someData);
}
}
public class DocumentsController : Controller
{
public ActionResult DoSomething()
{
TempData["SomeData"] = "Hello, world!";
return RedirectToAction("Index", "Home");
}
}
當你第一次訪問Home/Index
,"SomeData"
將丟失(空)。當您訪問Documents/DoSomething
時,它會將"SomeData"
設置爲一個字符串,然後將您重定向到Home/Index
。此時,Index
將看到我們放置在"SomeData"
中的字符串,您可以在您的Index
視圖中使用它。 然而,在之後,所有臨時數據將被清除。
例如,如果用戶在重定向一堆之後刷新Index
,則在刷新期間臨時數據將會丟失。如果這是不可接受的,那麼不要使用TempData,而是將其保留在Session中。
在contollres之間傳遞模型是乾淨和有效的 – 1AmirJalali
因此從我的DocumentsController操作傳遞路由值? – Mantorok
這會在一定程度上有所幫助..... http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3。最好的方法是使用model.binder方法來保留郵件中的值。 – ankur