Asp.net mvc MVC:动态填充html选择

Asp.net mvc MVC:动态填充html选择,asp.net-mvc,html.dropdownlistfor,html-select,Asp.net Mvc,Html.dropdownlistfor,Html Select,我为视图中的每一行生成了一个字段,如下所示: @Html.DropDownListFor(m => m.MenuParentKey, ViewBag.MenuKeys as SelectList) 但我希望通过在控制器中调用操作,为每一行单独填充下拉列表,我的意思如下: public ActionResult GetMenuKeys(string currentMenuKey) { var dictionary = MenuKeys.Where(mk => mk.K

我为视图中的每一行生成了一个字段,如下所示:

@Html.DropDownListFor(m => m.MenuParentKey, ViewBag.MenuKeys as SelectList)      
但我希望通过在控制器中调用操作,为每一行单独填充下拉列表,我的意思如下:

public ActionResult GetMenuKeys(string currentMenuKey)
{
  var dictionary = MenuKeys.Where(mk => mk.Key != currentMenuKey);

  return View(new SelectList(dictionary,
      "Key",
      "Value",
      Page.DefaultParentKey));
}

currentMenuKey
应根据当前行提供,我希望当前行的值从下拉列表中排除(请注意,这是父键属性)。

您可以创建一个助手,该助手将返回一个
SelectList

这将允许您直接从视图中调用它,您还可以根据需要传递参数

我认为这比对你的控制者采取行动要容易

从我在这里看到的情况来看,controler操作不会获取列表以外的任何其他资源

你可以这样做:

public static SelectList FilteredList( this HtmlHelper helper, MenuKeys keys, string CurrentMenuKey)
{
var dictionary = MenuKeys.Where(mk => mk.Key != currentMenuKey);

  return new SelectList(dictionary,
      "Key",
      "Value",
      Page.DefaultParentKey);
}
那么你可以这样称呼它:

@Html.DropDownListFor(m => m.MenuParentKey, Html.FilteredList(ViewBag.MenuKeys as SelectList, m.currentKey))    

您可以创建一个助手,该助手将返回一个
SelectList

这将允许您直接从视图中调用它,您还可以根据需要传递参数

我认为这比对你的控制者采取行动要容易

从我在这里看到的情况来看,controler操作不会获取列表以外的任何其他资源

你可以这样做:

public static SelectList FilteredList( this HtmlHelper helper, MenuKeys keys, string CurrentMenuKey)
{
var dictionary = MenuKeys.Where(mk => mk.Key != currentMenuKey);

  return new SelectList(dictionary,
      "Key",
      "Value",
      Page.DefaultParentKey);
}
那么你可以这样称呼它:

@Html.DropDownListFor(m => m.MenuParentKey, Html.FilteredList(ViewBag.MenuKeys as SelectList, m.currentKey))