【问题标题】:How can I filter a list with a dropdown list如何使用下拉列表过滤列表
【发布时间】:2013-07-04 02:15:21
【问题描述】:

我是 MVC 的新手,我想在我的观点上做两件事:

1 - 数据列表(3 列) 2 - 我可以过滤列表的下拉列表(填充第一列的数据)

在我的控制器中,我有以下功能:

public ViewResult ListUrl()
{
    var ws = new Service1();
    localhost.Service1 s1 = new Service1(); // data from web services
    localhost.UrlInfo[] ui = s1.GetUrlInfo();
    for (int i = 0; i < ui.Length; i++)
        {
            var UrlItem = new UrlItem();
            UrlItem.Id = Convert.ToInt32(ui[i].Id);
            UrlItem.urlll = ui[i].url;
            UrlItem.toontijd = ui[i].ToonTijd;
            UrlItem.positie  = Convert.ToInt32(ui[i].positie);

            Models.ListUrl.UrlList.Add(UrlItem);
        }

        var urlname = from url in s1.GetUrlInfo() select url  ;
        ViewData["url"] = new SelectList(urlname, "Id", "url");

    return View();

}

在视图中:

<script type="text/javascript">
$(document).ready(function () {

   // How can I filter the list (see <table> tag) when I change index of dropdown list???

});
</script>

@Html.DropDownList("SelectedItem", (SelectList)ViewData["url"], "----- all ------", new { id = "0", text = "----- all ------" })

<table>
<tr>
    <th>
        Url
    </th>
    <th>
        Toontijd
    </th>
    <th>
        Positie
    </th>
</tr>

@foreach (var item in ListUrl.UrlList)
{
    <tr>
        <td>
            @item.urlll.ToString()        
        </td>
        <td>
            @item.toontijd.ToString()

        </td>
        <td>

        </td>
        <td>
            @item.positie.ToString()
        </td>
    </tr>

}

如何让下拉列表更改事件起作用? 非常感谢。

希查姆。

【问题讨论】:

  • 我不是 MVC 专家,但我知道下拉列表有“autopostback”属性,尝试将其设置为“true”并绑定事件“yourdropdownlist_SelectedIndexChanged”,希望对您有所帮助
  • @losSebos: autopostback-and-MVC?!不知道的请不要推荐..
  • @Paritosh:我知道:stackoverflow.com/questions/730800/… 然后使用回发事件调用你的函数

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

嗯..您需要为此做一些事情..让我逐步解释..

  1. 为网格创建局部视图
  2. 为下拉菜单附加 onchange 事件
  3. 制作一个以下拉选择为参数并返回网格部分视图作为结果的控制器动作方法

    $.get('yourActionURL', { parameter: $('#yourDropdownId').val() }, function(result) {
        $('#grid').html(result);
    });
    

Filtering a WebGrid with a DropDownList in MVC4ASP.NET MVC Filtering results in a list/grid - 这些链接可以帮助您详细了解这一点。

【讨论】:

    猜你喜欢
    • 2015-11-02
    • 2019-05-20
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 2014-03-03
    相关资源
    最近更新 更多