MVC DropDownList滞后 [英] MVC DropDownList lagging

查看:49
本文介绍了MVC DropDownList滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将下拉列表的ID发回到索引(index2视图).但落后了.再按一次Select(选择)按钮,它会向我显示正确的列表.

http://www.jeroenchristens.be/CountriesWorld (第一页仅用于显示完整列表,从下拉列表中进行选择后,它会进入index2(一个较短的列表).然后从下拉列表中选择另一个选择"后,您每次必须尝试两次.

我成功地从id中复制了该值并将其传递给它,为什么它滞后了.

Index2 Viewpage

@using System.Collections
@using System.Web.UI.WebControls
@model IEnumerable<CVtje.Models.Countries>


<h2>Index</h2>
@using (Html.BeginForm("Index2", "CountriesWorld", new { @id = Request.Form["SelectedContinent"] }, FormMethod.Post))
{
    <div class="form-group">

        @Html.DropDownList("SelectedContinent", 
            new SelectList((IEnumerable) ViewData["continentsList"], "Continent", "Continentomschrijving"))
        <button type="submit" class="btn btn-primary">Select</button>
    </div>
}
    <table id="countriesworld" class="table table-active table-hover">
        <thead>
            <tr>
                <th>Vlag</th>
                <th>
                    Code
                </th>
                <th>
                    Land
                </th>
                <th>Continent</th>
            </tr>
        </thead>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    <img src="@string.Format("../../images/countries/{0}.png", item.Code)" width="25" HEIGHT="15" />

                </td>
                <td>

                    @item.Code



                </td>


                <td>
                    @item.Country
                    @*@Html.ActionLink("Details", "Index", "ReizensDetails", new { id = item.ReizenId }, null)*@


                    @*|
                        @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                        <button data-myprofile-id="@item.Id" class="btn-link js-delete">Delete</button>*@
                </td>
                <td>@item.Continents.Continentomschrijving</td>
            </tr>
        }

    </table>

我的控制器:

public ActionResult Index(int? id)
{


    List<Continents> continentsList = new List<Continents>();
    continentsList = _context.Continents.ToList();
    ViewData["continentsList"] = continentsList;

    var countriesWorld = _context.Countries.OrderBy(e => e.Country).ToList();

    return View(countriesWorld);

}

[HttpPost]
public ActionResult Index2(int id)
{

    //return View(db.MyProfiles.ToList());
    List<Continents> continentsList = new List<Continents>();
    continentsList = _context.Continents.ToList();
    ViewData["SelectedContinent"] = id.ToString();
    ViewData["continentsList"] = continentsList;



        var countriesWorld = _context.Countries.Where(e => e.Continent == id).OrderBy(e => e.Country).ToList();

    return View(countriesWorld);

解决方案

您已经在BeginForm()方法中使用new { @id = Request.Form["SelectedContinent"] }添加了路由值.

假设初始值为0,则它将生成action = "/CountriesWorld/Index2/0".假设您使用value="1"选择选项,然后发布表单. id属性绑定到0,并且您基于.Where(e => e.Continent == 0)过滤国家/地区-在哪里都没有使用所选选项的值绑定到一个不存在的名为SelectedContinent的属性. >

现在,您返回视图,并且表单action的属性现在为action = "/CountriesWorld/Index2/1"(因为Request.Form["SelectedContinent"]1).如果使用value="2"选择选项,则会发生相同的事情-您忽略所选选项的值,并过滤基于.Where(e => e.Continent == 1)的国家/地区,因为id参数是1.

始终绑定到一个模型,在您的情况下将是

public class CountriesVM
{
    public int? SelectedContinent { get; set }
    public IEnumerable<SelectListItem> ContinentsList { get; set; }
    public IEnumerable<Country> Countries { get; set; }
}

并在视图中牢固绑定到模型(请注意FormMethod.GetDropDownListFor()中的第3个参数)

@model CountriesVM
@using (Html.BeginForm("Index", "CountriesWorld", FormMethod.Get))
{
    @Html.DropDownListFor(m => m.SelectedContinent, Model.ContinentsList, "All")
    <button type="submit" class="btn btn-primary">Select</button>
}
<table ... >
    ....
    @foreach(var country in Model.Countries)
    {
        ....
    }
 </table>

您只需要一种方法

public ActionResult Index(int? selectedContinent)
{
    var countries = _context.Countries.OrderBy(e => e.Country);
    if (selectedContinent.HasValue)
    {
        countries = countries.Where(e => e.Continent == selectedContinent.Value);
    }
    continentsList = _context.Continents.Select(x => new SelectListItem
    {
        Value = x.Continent.ToString(),
        Text = x.Continentomschrijving
    });
    var model = new CountriesVM
    {
        SelectedContinent = selectedContinent,
        ContinentsList = continentsList,
        Countries = countries
    };
    return View(model);
}

请注意,您可能还想考虑缓存Continents,以防止重复的数据库调用(假设它们不经常更改)(并且如果更新其值,则使缓存无效)

I am posting the id of a dropdownlist back to the index (index2 view). but is lagging behind. After a second time pressing Select it shows me the correct list.

http://www.jeroenchristens.be/CountriesWorld (the first page is only for showing the complete list, after selecting from the dropdownlist,, it gets to index2, a shorter list) And then after choosing another Selection from the dropdownlist, you have to try this twice each time.

I successfully copied this from the id the value and pass this on, why is it lagging behind.

Index2 Viewpage

@using System.Collections
@using System.Web.UI.WebControls
@model IEnumerable<CVtje.Models.Countries>


<h2>Index</h2>
@using (Html.BeginForm("Index2", "CountriesWorld", new { @id = Request.Form["SelectedContinent"] }, FormMethod.Post))
{
    <div class="form-group">

        @Html.DropDownList("SelectedContinent", 
            new SelectList((IEnumerable) ViewData["continentsList"], "Continent", "Continentomschrijving"))
        <button type="submit" class="btn btn-primary">Select</button>
    </div>
}
    <table id="countriesworld" class="table table-active table-hover">
        <thead>
            <tr>
                <th>Vlag</th>
                <th>
                    Code
                </th>
                <th>
                    Land
                </th>
                <th>Continent</th>
            </tr>
        </thead>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    <img src="@string.Format("../../images/countries/{0}.png", item.Code)" width="25" HEIGHT="15" />

                </td>
                <td>

                    @item.Code



                </td>


                <td>
                    @item.Country
                    @*@Html.ActionLink("Details", "Index", "ReizensDetails", new { id = item.ReizenId }, null)*@


                    @*|
                        @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                        <button data-myprofile-id="@item.Id" class="btn-link js-delete">Delete</button>*@
                </td>
                <td>@item.Continents.Continentomschrijving</td>
            </tr>
        }

    </table>

my controller:

public ActionResult Index(int? id)
{


    List<Continents> continentsList = new List<Continents>();
    continentsList = _context.Continents.ToList();
    ViewData["continentsList"] = continentsList;

    var countriesWorld = _context.Countries.OrderBy(e => e.Country).ToList();

    return View(countriesWorld);

}

[HttpPost]
public ActionResult Index2(int id)
{

    //return View(db.MyProfiles.ToList());
    List<Continents> continentsList = new List<Continents>();
    continentsList = _context.Continents.ToList();
    ViewData["SelectedContinent"] = id.ToString();
    ViewData["continentsList"] = continentsList;



        var countriesWorld = _context.Countries.Where(e => e.Continent == id).OrderBy(e => e.Country).ToList();

    return View(countriesWorld);

解决方案

You have added a route value using new { @id = Request.Form["SelectedContinent"] } in your BeginForm() method.

Assuming the initial value is 0, then it generates action = "/CountriesWorld/Index2/0". Lets assume you select the option with value="1" and you now post the form. The id attribute is bound to 0 and you filter the Countries based on .Where(e => e.Continent == 0) - no where have you ever used the value of the selected option which is bound to a non-existent property named SelectedContinent.

Now you return the view and the forms action attribute is now action = "/CountriesWorld/Index2/1" (because Request.Form["SelectedContinent"] is 1). If you select the option with value="2", the same thing occurs - you ignore the value of the selected option and the filter the Countries based on .Where(e => e.Continent == 1) because the id parameter is 1.

Always bind to a model, which in your case will be

public class CountriesVM
{
    public int? SelectedContinent { get; set }
    public IEnumerable<SelectListItem> ContinentsList { get; set; }
    public IEnumerable<Country> Countries { get; set; }
}

and in the view, strongly bind to your model (note the FormMethod.Get and the 3rd parameter in DropDownListFor())

@model CountriesVM
@using (Html.BeginForm("Index", "CountriesWorld", FormMethod.Get))
{
    @Html.DropDownListFor(m => m.SelectedContinent, Model.ContinentsList, "All")
    <button type="submit" class="btn btn-primary">Select</button>
}
<table ... >
    ....
    @foreach(var country in Model.Countries)
    {
        ....
    }
 </table>

and you need only one method

public ActionResult Index(int? selectedContinent)
{
    var countries = _context.Countries.OrderBy(e => e.Country);
    if (selectedContinent.HasValue)
    {
        countries = countries.Where(e => e.Continent == selectedContinent.Value);
    }
    continentsList = _context.Continents.Select(x => new SelectListItem
    {
        Value = x.Continent.ToString(),
        Text = x.Continentomschrijving
    });
    var model = new CountriesVM
    {
        SelectedContinent = selectedContinent,
        ContinentsList = continentsList,
        Countries = countries
    };
    return View(model);
}

Note you might also want to consider caching the Continents to avoid repeated database calls assuming they do not change often (and invalidate the cache if their values are updated)

这篇关于MVC DropDownList滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆