views:

158

answers:

1

The ASP.net page I am currently working on has a drop down list that is intended to have a list of filters. When the user selects the filter, I would like to display a user control that has properties appropriate for the filter.

Here is the controller action in question:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection collection)
{
  var filterType =  Request.Form["FilterSelect"];
  ViewData["FilterChosen"] = filterType;
  PopulateSelectionFiltersData();//This method fills up the drop down list
  //Here is where I would like to switch based on the filterType variable
  return View();
}

The filter type variable has the correct value, but I'm unsure as to how to do the next part.

Also, as a corollary question, what would be the best way to persist the selected drop down value between calls?

Many thanks,

KevDog

+3  A: 

Store the correct control to display in ViewData. As for persisting the menus, your choices are the Cache (used by many sessions), Session (used only by this session), or TempData (used only for the next method in this session). Alternatively, you could have it cached in your DataLayer. Typically, I just refetch the data until it becomes a performance issue -- which it usually doesn't.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection collection)
{
  var filterType =  Request.Form["FilterSelect"];
  ViewData["FilterChosen"] = filterType;
  PopulateSelectionFiltersData();//This method fills up the drop down list

  string userControl = "DefaultControl";
  switch (filterType)
  {
      case "TypeA":
         userControl = "TypeAControl";
         break;
      ...
  }

  ViewData["SelectedControl"] = userControl; 
  return View();
}


 <% Html.RenderPartial( ViewData["SelectedControl"], Model, ViewData ); %>
tvanfosson
Need to change the RenderPartial to eliminate the equals sign and add a semicolon at the end. Otherwise, this is perfect.
KevDog
@KevDog - nice catch. Corrected.
tvanfosson