简体   繁体   English

DropDownList Model 绑定 ASP.NET PagedList 视图中的 MVC

[英]DropDownList Model Binding ASP.NET MVC in a PagedList View

I need to bind the model with the dropdownlist to create a custom sorting filters.我需要将 model 与下拉列表绑定以创建自定义排序过滤器。
My view receives the "@model PagedList.IPagedList<EmployeeInvestments.Models.Registration>" as model.我的视图收到“@model PagedList.IPagedList<EmployeeInvestments.Models.Registration>”作为 model。

A part of My View:-我的观点的一部分:-

@model PagedList.IPagedList<EmployeeInvestments.Models.Registration>
@using PagedList.Mvc;
@{
    ViewBag.Title = "AdminDashboard";
}

<div>
    Financial Year:
    <select>
        @foreach(var item in Model)
        {
            <option>@Html.ValueFor(ModelItem=>item.FinancialYear)</option>
        }
    </select>

        Name:
        <select>
            @foreach (var item in Model)
            {
                <option>@Html.ValueFor(ModelItem => item.Name)</option>
            }
        </select>
        Employee ID:
        <select>
            @foreach(var item in Model)
            {
                <option>@Html.ValueFor(ModelItem=>item.employeeID)</option>
            }
        </select>
</div>

I tried the above approach for getting items in dropdownlist but I need unique values in the dropdownlist.我尝试了上述方法来获取下拉列表中的项目,但我需要下拉列表中的唯一值。

The image shows what exactly I want to achieve in View该图像显示了我想要在 View 中实现的目标

I have created a table in the view that lists all users and certain details.我在列出所有用户和某些详细信息的视图中创建了一个表。 So, I need to sort the users based on the filters available in dropdownlist.因此,我需要根据下拉列表中可用的过滤器对用户进行排序。

Please refer to image for getting a clear picture of what I need to achieve.请参考图片以清楚了解我需要实现的目标。

To filter by distinct year:按不同年份过滤:

Financial Year:
<select>
    @foreach(var item in Model.Select(x => x.FinancialYear).Distinct())
    {
        <option>@Html.ValueFor(ModelItem=>item.FinancialYear)</option>
    }
</select>

To get Unique date you can use groupby要获得唯一日期,您可以使用 groupby

<select>
    @foreach(var item in Model.GroupBy(i=>i.FinancialYear).Select(x => x.Key))
    {
        <option>@Html.ValueFor(ModelItem=>item)</option>
    }
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM