简体   繁体   English

更改MVC SelectList选择的值

[英]Changing MVC SelectList selected value

SelectList dropdown = DropDown;
foreach (var item in dropdown)
    {
     var modelValue = property.GetValue(Model.FormModel);
     if (String.Equals(item.Value, modelValue))
              {
                   item.Selected = true;
                   System.Diagnostics.Debug.WriteLine(item.Selected);
               }
     }

foreach (var item in dropdown)
      {
       var modelValue = property.GetValue(Model.FormModel);
       if (String.Equals(item.Value, modelValue))
             {
                    System.Diagnostics.Debug.WriteLine(item.Selected);
              }
       }

Logically, the code above should output either nothing or true, true unless magic magnetic fields are changing bits in the computer between one foreach loop and the other. 从逻辑上讲,除非魔术磁场在一个foreach循环和另一个foreach循环之间改变计算机中的位,否则上面的代码应输出nothing或true, true

And yet, I get true, false . 然而,我得到了true, false How is this even remotely possible? 这怎么可能远程可能? With the debugger, I saw 'item' gets correctly parsed and item.Selected = true gets correctly called on the item I'd like. 使用调试器,我看到'item'被正确解析,并且item.Selected = true在我想要的项目上被正确调用。 The second loop is just for debugging purpose. 第二个循环仅用于调试目的。


this is how I build the DropDown. 这就是我构建DropDown的方法。 I can't touch this code as the dropdown returned should always be generic. 我无法触及此代码,因为返回的下拉列表应始终是通用的。

var prov = (from country in Service.GetCountries()
         select new
          {
           Id = country.Id.ToString(),
           CountryName = Localizator.CountryNames[(CountryCodes)Enum.Parse(typeof(CountryCodes), country.Code)],
           }).Distinct().ToList().OrderBy(l => l.CountryName).ToList();
           prov.Insert(0, new { Id = String.Empty, CountryName = Localizator.Messages[MessageIndex.LabelSelectAll] });
  _customerCountrySelectionList = new SelectList(prov, "Id", "CountryName");

if you iterate through a collection using foreach you can not modify its contents. 如果使用foreach迭代集合,则无法修改其内容。 therefore the 2nd iteration will access the same unmodified list... 因此第二次迭代将访问相同的未修改列表...

use Linq to create a list of "SelectListItems" directly and then assign that list to the dropdownhelper 使用Linq直接创建“SelectListItems”列表,然后将该列表分配给dropdownhelper

from x in y where ... select new SelectListItem { value = ..., text = ..., selected = ... }

using your code... you might want to create something like 使用你的代码...你可能想要创建类似的东西

var modelValue = property.GetValue(Model.FormModel);
IEnumerable<SelectListItem> itemslist = 
         (from country in Service.GetCountries()
          select new SelectListItem {
          {
            value = country.Id.ToString(),
            text  = Localizator
                      .CountryNames[
                          (CountryCodes)Enum
                                         .Parse(typeof(CountryCodes),
                          country.Code)
                       ],
            selected = country.Id.ToString().Equals(modelValue)
           }).Distinct().ToList().OrderBy(l => l.text);

... havent tested that in VS though, so play around with it and see if you can get it to work ...虽然没有在VS中测试过,所以请玩它并看看你是否可以让它工作

item.Selected = true; item.Selected = true; is set to true on first loop. 在第一个循环中设置为true。

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

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