我有一個基於以下情形幾個問題:跨控制器傳遞視圖模型(含請求參數)
我有以下方法的LoginController:
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(UserObject user)
{
Calling Besiness Service to Validate User againts DB (Using Repository)...
if (success)
{
return RedirectToAction("Search", "Search");
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
return View(user);
}
登錄成功後,我要提出一個搜索屏幕。我有以下方法SearchController:
public ActionResult Search()
{
return View(); // This returns a EMPTY Search Form.
}
[HttpPost]
public ActionResult Search(SearchView search)
{
// Validate Search parameters...
// I have to perform a search by calling Business Service and present results. I would like to use a seperate controller (PricingController) for this task.
return RedirectToAction("Index", "Pricing"); // Not sure if this is the correct way?
}
現在我想有一個PricingController將採取「搜索查看」視圖模型包含我的搜索參數,並調用業務服務,以獲得定價結果,並將其呈現給用戶。
這是一個正確的方法嗎?如何將包含我的搜索參數的「SearchView」ViewModel從SearchController傳遞給PricingController?我需要在整個應用程序中訪問(數據)「SearchView」ViewModel,因爲用戶可以根據初始搜索條件添加可選項目,並且可以根據需要修改此搜索條件。我應該將「SearchView」ViewModel存儲在緩存中嗎?我將在Web Farm中部署我的應用程序,並且不想使用Http Session。我將使用Cookies來管理用戶會話。
謝謝Joakim。我知道我可以這樣做,但這是一種正確的方法嗎?還是有更好的方法來做到這一點? – Alex 2010-10-18 18:05:44
據我所知,如果你想基於另一個視圖返回一個視圖,那麼這就是要走的路,然而我會直接在動作搜索中使用包含搜索方法的服務,並從那裏呈現視圖。 – Joakim 2010-10-18 18:27:57
再次感謝。關於您的第二種方法,我可以在「搜索(SearchView搜索)」方法中調用定價服務並填充PricingView View模型,但是如何呈現View?如果我調用「返回視圖(模型)」,那麼它不會改變我的瀏覽器中的URL,並在瀏覽器上重新加載/刷新將是一個問題...如果我調用「RedirectToAction()」方法,那麼我在我的視圖模型。我希望我有道理? – Alex 2010-10-18 19:28:13