筛选器ViewModel不起作用ASP MVC

时间:2018-10-22 13:15:34

标签: c# asp.net-mvc post filter viewmodel

我想用dropdownlist过滤数据。为此,我创建了一个ViewModel。我可以显示数据,但过滤器不起作用。当我选择一个国家作为示例时,我会在网格中看到所有国家的清单。就像HttpPost在控制器中不起作用...

在我的视图模型中,我有以下脚本:

    public class IndexGetViewModel
{
    public List<missions_supportmission> missions_supportmission { get; set; }
    public missions_supportmission missions { get; set; }
    public  SelectList CountryList { get; set;  }
    public string CountryIdSelected { get; set; }
}

在控制器中,我具有以下脚本:

        private Missions_devEntities db = new Missions_devEntities();

    //
    // GET: /Missions/Index

    public ActionResult Index()
    {
        IndexGetViewModel model = new IndexGetViewModel();
        //Affichage de geo division
        model.CountryList = new SelectList(this.db.organization_hi_country.ToDictionary(b => b.country_id, b => b.name_en), "Key", "Value");
        model.missions_supportmission = db.missions_supportmission.ToList() ;
        return View(model);          
    }

    //POST: /Missions/Index
    [HttpPost]
    public ActionResult Index(IndexGetViewModel GetVM)
    {
        missions_supportmission missions = GetVM.missions;
        //on cherche le le pays sélectionné dans la table missions
        missions.organization_hi_country = this.db.organization_hi_country.Find(GetVM.CountryIdSelected);
        //On affiche les pays dans la table missions
        GetVM.missions_supportmission = db.missions_supportmission.Include(s => s.organization_hi_country).Where(s => s.organization_hi_country.country_id == GetVM.CountryIdSelected).ToList();
        return View(GetVM);
    }

我认为我有以下脚本:

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

        <section>
            <h3>Localisation</h3>
            <div class="form-group">
                @Html.LabelFor(model => model.CountryIdSelected, "Country", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(model => model.CountryIdSelected, Model.CountryList, htmlAttributes: new { onchange = "form.submit();", @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.CountryIdSelected, "", new { @class = "text-danger" })
                </div>
            </div>
        </section>
    }
</nav>

    <div id="content" class="container">
        @using (Html.BeginForm("Index", "Missions", FormMethod.Get))
        {
            @Html.TextBox("searching")
            <input type="submit" value="submit" />
        }

<div>
    <h2>Missions</h2>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table table-bordered">
        @using (Html.BeginForm("Index"))
        {
            <tr>
                <th class="col-md-2">
                    Pays
                </th>
                <th class="col-md-1">
                    Date
                </th>
                <th class="col-md-2">
                    Personne
                </th>
                <th class="col-md-10">
                    Missions
                </th>
                <th class="col-md-10">
                </th>
            </tr>
        }

        @foreach (var item in Model.missions_supportmission)
        {
            <tr>
                <td class="col-md-2">
                    @Html.DisplayFor(modelItem => item.organization_hi_country.name_en)
                </td>

                    <td class="col-md-1">
                        @Html.DisplayFor(modelItem => item.asked_date)
                    </td>
                    <td class="col-md-2">
                        @Html.DisplayFor(modelItem => item.type_mission)
                    </td>
                    <td class="col-md-10">
                        Mission @Html.DisplayFor(modelItem => item.list_type.name_en) : @Html.DisplayFor(modelItem => item.list_nature.name_en)
                        <div>
                            <!-- les boutons d'actions -->
                            <a href="#@Html.DisplayFor(modelItem => item.id)" data-toggle="collapse" aria-expanded="false" aria-controls="@Html.DisplayFor(modelItem => item.id)">details</a>
                        </div>
                        <!-- le contenu masqué -->
                        <section id="@Html.DisplayFor(modelItem => item.id)" class="collapse">
                            <div class="well">
                                <p><em>Statut: </em>@Html.DisplayFor(modelItem => item.list_statut.name_en)</p>
                                <p><em>Durée: </em>@Html.DisplayFor(modelItem => item.duration) jours</p>
                                <p><em>Flexibilité: </em>@Html.DisplayFor(modelItem => item.list_flexibility.name_en) </p>
                                <p><em>Priorité: </em>@Html.DisplayFor(modelItem => item.list_priority.name_en) </p>
                                <p><em>Commentaires: </em>@Html.DisplayFor(modelItem => item.comments) </p>
                            </div>
                        </section>

                    </td>
                    <td class="col-md-10">
                        @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ })
                        @Html.ActionLink("Details", "Details", new { id = item.id })
                        @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
                    </td>

            </tr>
        }

    </table>

1 个答案:

答案 0 :(得分:0)

您使用的是FormMethod.Get,而不是FormMethod.Post