views:

62

answers:

3

I have few questions based on following scenario:

I have a LoginController with following methods:

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);
}

After successful login, I have to present a Search screen. I have a SearchController with following methods:

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?
}

Now I would like to have a PricingController which will take "SearchView" ViewModel containing my search parameters and call a Business Service to get the pricing results and present it to the user.

Is this a correct approach? How do I pass the "SearchView" ViewModel containing my search parameters to PricingController from SearchController? I need access (data) to "SearchView" ViewModel throughout the application because user can add optional items based on initial search criteria and can modify this search criteria if required. Should I store "SearchView" ViewModel in Cache? I will be deploying my application in Web Farm and don't want to use Http Session. I will be using Cookies for managing user sessions.

A: 

to pass on the model to another action you can do this:

[HttpPost]
public ActionResult Search(SearchView search)
{
    return RedirectToAction("Index", "Pricing", new { exactNameOfSearchViewParameter = search });
}
Joakim
Thanks Joakim. I know I can do that but is that a correct approach? or is there a better way of doing this?
Alex
to my knowledge this is the way to go if you want to return a view based on another view, I however would use the service which contains the search method directly in the action Search and render the view from there.
Joakim
Thanks again. Regarding your second approach, I can call the Pricing Service inside "Search(SearchView search)" method and populate a PricingView View model but then how do I render the View? If I call "return View(Model)" then it won't change my URL in browser and hitting reload/refresh on browser would be an issue... If I call "RedirectToAction()" method then I loose data in my View Model. I hope I am making sense???
Alex
there are ways, but after reading more I would've had the search function in the service layer and have it return a result based on the search (i usually use something like public IEnumerable<theObject>(string search, string splitOperand)) and have the Index ActionResult in the PricingController to take a querystring to display the result. But this is just how i would do it.
Joakim
A: 

You could have a pricing repository which deals with calculations of prices. Now inject this repository into each controller which needs to calculate prices and use it in the actions. This way you don't need any redirects, in each action which needs to work with prices simply call the appropriate method on your repository.

Darin Dimitrov
Thanks Darin. Its actually the "SearchView" ViewModel containing my search parameters which I need in multiple (70%) controllers and my Pricing results for Core components, add-ons, midfications etc would be based on the initial search criteria. Would you suggest injecting this information into my controllers? If so, what is the best way to inject this info?
Alex
You could write a custom controller factory and [inject dependencies into your controllers](http://weblogs.asp.net/shijuvarghese/archive/2009/03/12/applying-dependency-injection-in-asp-net-mvc-nerddinner-com-application.aspx).
Darin Dimitrov
A: 

In this case, I think I would move the Search action method to the PricingController. It seems like it really belongs there, as you are doing a price-related search.

Then you don't have to worry about how to transfer data between controllers.

You can modify your view to post directly there:

<% Html.BeginForm("search", "pricing"); %>
...
<% Html.EndForm() %>

If you really need to pass data between controllers or actions, you can use TempData. Just make sure your controller / action / view still works when TempData is empty.

Thomas Eyde