我目前正在处理的ASP.net页面有一个下拉列表,其中包含一个过滤器列表。当用户选择过滤器时,我想显示一个具有适合过滤器的属性的用户控件。

以下是有问题的控制器操作:

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

过滤器类型变量具有正确的值,但我不确定如何进行下一部分。

另外,作为一个必然结果,在调用之间保持所选下拉值的最佳方法是什么?

非常感谢,

KevDog

有帮助吗?

解决方案

存储正确的控件以在ViewData中显示。至于坚持菜单, 您的选择是Cache(由多个会话使用),Session(仅由此会话使用)或TempData(仅用于此会话中的下一个方法)。或者,您可以将其缓存在DataLayer中。通常,我只是重新获取数据,直到它成为性能问题 - 通常不会。

[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 ); %>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top