【问题标题】:How to use a @Html.DropDownList with ViewBag?如何将 @Html.DropDownList 与 ViewBag 一起使用?
【发布时间】:2013-05-27 14:02:28
【问题描述】:

我正在使用 MVC4。

我在一个Index页面中添加了分页、排序和过滤功能。因此,我必须在过滤DropdownList中添加一个viewbag参数来保持过滤字符串。

下面是一个带有 viewbag 参数的文本框示例:

@Html.TextBox("name",ViewBag.CurrentFilterName as string) 

这很好用。

我想将 viewbag 参数添加到 DropDownList。 这是我的这个 DropDownList 的 HTML 代码:

@Html.DropDownList("type","All",ViewBag.CurrentFilterType as string).

这是错误的。

谁能告诉我如何纠正它? 非常感谢!

【问题讨论】:

  • 我设法找到了解决方案?我可以帮忙吗?

标签: asp.net-mvc filtering html-select viewbag


【解决方案1】:

可以想象他们可以是:@Html.DropDownList("type", ViewBag.CurrentFilterType)

检查这个:MVC @Html.DropDownList Getting Error with SelectList in ViewBag

【讨论】:

    【解决方案2】:

    试试这个代码: 查看:

    <td id="dropdown">
        @Html.DropDownList("Parameter", ViewBag.Parameter as SelectList)
    </td>
    

    控制器:

    public ActionResult Index()
    {
        ViewBag.Parameter = new SelectList(_dataModel.GetWeightage(), "Parameter");
        return View();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多