使用下拉列表和Viewmodel在视图中过滤网格

时间:2018-10-16 12:35:35

标签: c# html asp.net-mvc filtering viewmodel

我想用viewmodel中的dropdownlist过滤网格中的数据,但是我不知道该怎么做。 我想创建这样的控制器: 我的控制器:

public ActionResult Index()
    {
        var missions = this.db.missions_supportmission
            .Include(m => m.organization_desk)
            .Include(m => m.organization_programme)
            .Select(x => new MissionsViewModel()
            {                   
                DeskIdSelected = x.organization_desk.name_en,
                ProgrammeIdSelected = x.organization_programme.name_en,
                DeskList = new SelectList(this.db.organization_desk.ToDictionary(b => b.desk_id, b => b.name_en), "Key", "Value"),
                ProgrammeList = new SelectList(this.db.organization_programme.ToDictionary(b => b.prog_id, b => b.name_en), "Key", "Value")
    });

在视图模型中,我有这个:

public class MissionsViewModel
{
    public missions_supportmission missions_supportmission { get; set; }

    [Required]
    public string DeskIdSelected { get; set; }

    [Required]
    public string ProgrammeIdSelected { get; set; }

    [Required]
    public string CountryIdSelected { get; set; }

    [Required]
    public string NatureIdSelected { get; set; }

    [Required]
    public string DirectionIdSelected { get; set; }

    [Required]
    public string UnitIdSelected { get; set; }

    [Required]
    public string TypeIdSelected { get; set; }

    public SelectList DeskList { get; set; }

    public SelectList ProgrammeList { get; set; }
    public SelectList TypeList { get; set; }
    public SelectList CountryList { get; set; }
    public SelectList NatureList { get; set; }
    public SelectList DirectionList { get; set; }
    public SelectList UnitList { get; set; }
}

我的视图(在下拉列表中,我使用了第一个viewbag,但是我不再想要使用它。我想使用强类型代码)

@model IEnumerable<MissionsDF.Models.MissionsViewModel>


        
            
                

Filtres

@using (Html.BeginForm("Index", "Missions", FormMethod.Get)) {

Localisation

@Html.LabelFor(model => model, "Desk", htmlAttributes: new { @class = "control-label col-md-4" }) @Html.DropDownList("SelectedDesk", new SelectList(ViewBag.Listedesdesks, "Value", "Text", "ViewBag.SelectedDesk"), "All", new { onchange = "form.submit();", @class = "form-control" }) }

Missions

@Html.ActionLink("Create New", "Create")

@using (Html.BeginForm("Index")) { @Html.ActionLink("Pays", "Index", new { SortOrder =ViewBag.ListorderPays, /*SelectedDesk = ViewBag.ListorderPays, SelectedProgramme = ViewBag.ListorderDate,*/ }) @Html.ActionLink("Date", "Index", new { SortOrder = ViewBag.ListorderDate, /*SelectedDesk = ViewBag.ListorderPays, SelectedProgramme = ViewBag.ListorderDate,*/ }) Personne Missions } @foreach (var item in Model) { @Html.DisplayFor(modelItem => item.DeskIdSelected) @Html.DisplayFor(modelItem => item.ProgrammeIdSelected) }

调试项目时出现以下错误

System.NotSupportedException: 'Only parameterless constructors and initializers are supported in LINQ to Entities.'

我知道错误的出处,但我不知道如何在控制器中编写我的代码,以便一次在网格中显示一个网格和一个下拉列表以使用viewmodel过滤数据。

感谢您的帮助!

0 个答案:

没有答案