我有一个基于以下情形几个问题:跨控制器传递视图模型(含请求参数)
我有以下方法的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