加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 使用下拉列表过滤MVC中的结果

发布时间:2020-12-16 07:18:27 所属栏目:asp.Net 来源:网络整理
导读:我有一个返回表的MVC Web应用程序.我想在视图页面中添加一个下拉列表,将表格过滤到所选年份.什么是正确的方法? 我正在创建一个我正在使用JQuery填充的下拉列表.我创建了一个onChange命令来回发,但我不知道如何在控制器中获取下拉列表的选定值. 以下是我的代
我有一个返回表的MVC Web应用程序.我想在视图页面中添加一个下拉列表,将表格过滤到所选年份.什么是正确的方法?

我正在创建一个我正在使用JQuery填充的下拉列表.我创建了一个onChange命令来回发,但我不知道如何在控制器中获取下拉列表的选定值.

以下是我的代码:

在控制器中:

public ActionResult Index()
    {
        int year = 2012;
        var vu_Estimates = dbBudget.vu_Estimates.OrderByDescending(o => o.Expense).ThenBy(o => o.CategoryGroupSortOrder).ThenBy(o => o.SortOrder).Where(o => o.Year == year).ToList();

        return View(vu_Estimates);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(FormCollection formCollection)
    {
        int year = 2012;
        var vu_Estimates = dbBudget.vu_Estimates.OrderByDescending(o => o.Expense).ThenBy(o => o.CategoryGroupSortOrder).ThenBy(o => o.SortOrder).Where(o => o.Year == year).ToList();

        return View(vu_Estimates);
    }

在视图中,我有以下内容:

<script type="text/javascript">
    $(document).ready(function () {        
        $.post("/Estimate/PopulateYears/",{ Year: $(this).val() },function (data) {
            populateDropdown($("#ddlYears"),data);
        });
    });

    function populateDropdown(select,data) {
        select.html('');
        $.each(data,function (id,option) {
            select.append($('<option></option>').val(option.value).html(option.name));
        });
    }
</script>

<h2>Estimates List</h2>

<div>
    <% using (Html.BeginForm()) { %>
        <select id="ddlYears" onchange="this.form.submit();"></select> | <%: Html.ActionLink("Create New Year of Estimates","CreateEstimates","Estimate") %>
    <%} %>
</div>


<table>
    <tr>
        <th></th>
        <th>
            EstimateID
        </th>
        <th>
            CategoryID
        </th>
        <th>
            Year
        </th>
        <th>
            EstimateAmount
        </th>
        <th>
            CategoryName
        </th>
        <th>
            SortOrder
        </th>
        <th>
            CategoryGroupSortOrder
        </th>
        <th>
            Expense
        </th>
    </tr>

<% foreach (var item in Model) { %>
    <tr>
        <td>
            <%: Html.ActionLink("Edit","Edit",new { id=item.EstimateID }) %> |
            <%: Html.ActionLink("Delete","Delete",new { id=item.EstimateID })%>
        </td>
        <td>
            <%: item.EstimateID %>
        </td>
        <td>
            <%: item.CategoryID %>
        </td>
        <td>
            <%: item.Year %>
        </td>
        <td>
            <%: item.EstimateAmount %>
        </td>
        <td>
            <%: item.CategoryName %>
        </td>
        <td>
            <%: item.SortOrder %>
        </td>
        <td>
            <%: item.CategoryGroupSortOrder %>
        </td>
        <td>
            <%: item.Expense %>
        </td>
    </tr>
<% } %>

</table>

<p>
    <%: Html.ActionLink("Create New","Create") %>
</p>

解决方法

首先,给你的下拉列表一个名字:

<div>
    <% using (Html.BeginForm()) { %>
        <select id="ddlYears" name="Years" onchange="this.form.submit();"></select> | <%: Html.ActionLink("Create New Year of Estimates","Estimate") %>
    <%} %>
</div>

然后使用FormCollection对象检索值:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection formCollection)
{
    int year = Convert.ToInt32(formCollection["Years"]);
    var vu_Estimates = dbBudget.vu_Estimates.OrderByDescending(o => o.Expense).ThenBy(o => o.CategoryGroupSortOrder).ThenBy(o => o.SortOrder).Where(o => o.Year == year).ToList();

    return View(vu_Estimates);
}

这样的事情.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读