代码之家  ›  专栏  ›  技术社区  ›  KevDog

动态选择ASP.NET MVC用户控件

  •  1
  • KevDog  · 技术社区  · 17 年前

    我当前正在处理的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();
    }
    

    filter-type变量的值是正确的,但我不确定如何执行下一部分。

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

    多谢,

    凯夫特

    1 回复  |  直到 17 年前
        1
  •  3
  •   tvanfosson    17 年前

    存储要在ViewData中显示的正确控件。至于菜单的保存, 您的选择是缓存(多个会话使用)、会话(仅此会话使用)或tempdata(仅用于此会话中的下一个方法)。或者,您可以将它缓存在数据层中。通常,我只是重新蚀刻数据,直到它成为性能问题——通常不会。

    [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 ); %>
    
    推荐文章