动态选择 Asp.Net MVC 用户控件

发布于 2024-07-17 22:47:24 字数 582 浏览 4 评论 0原文

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

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

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

非常感谢,

凯夫狗

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪菊怪哟 2024-07-24 22:47:24

存储要在 ViewData 中显示的正确控件。 至于保留菜单,
您的选择是 Cache(由许多会话使用)、Session(仅由该会话使用)或 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 ); %>

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 ); %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文