【问题标题】:Dynamically Choosing a Asp.Net MVC User Control动态选择 Asp.Net MVC 用户控件
【发布时间】:2023-04-09 16:15:01
【问题描述】:

我目前正在处理的 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

【问题讨论】:

    标签: c# asp.net-mvc asmx


    【解决方案1】:

    存储要在 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 ); %>
    

    【讨论】:

    • 需要改变RenderPartial去掉等号,并在末尾加分号。否则,这是完美的。
    • @KevDog - 不错的收获。已更正。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    • 2010-11-18
    • 2010-11-28
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    相关资源
    最近更新 更多