簡體   English   中英

Html.DropDownListFor沒有選擇正確的值

[英]Html.DropDownListFor doesn't select correct value

從帶有國家/地區的XML文件中生成Selectlist(IEnumerable),並將其與DropDownListFor一起使用,不會設置Model中提供的所選值嗎?

public static IEnumerable<SelectListItem> CountrySelectList()
    {
        var sRetVal = new List<SelectListItem>();
        string CachKey = "MVCXMLCountryList" + GetCulture();
        if (HttpContext.Current.Cache[CachKey] == null | 1 == 1)
        {
            string xmlFile = Path.Combine(HttpContext.Current.Server.MapPath("~"),      "Countries.xml");

            XmlDocument Doc = new XmlDocument();
            Doc.Load(xmlFile);
            foreach (XmlNode Node in    Doc.SelectNodes(String.Format("//Countries/language[@iso=\"{0}\"]/country", GetCulture())))
            {
                var tmpSelect = new SelectListItem();
                tmpSelect.Value = Node.Attributes["iso2"].InnerText.ToString();
                tmpSelect.Text = Node.Attributes["name"].InnerText;
                tmpSelect.Selected = false;
                sRetVal.Add(tmpSelect);
            }
            sRetVal.Sort(CountrySort);
            var prioritet = new string[] {"de","fo","se","no","dk"};
            switch (GetCulture())
            {
                case "dk": prioritet = new string[] {"de","fo","se","no","dk"}; break;
                default: prioritet = new string[] { "de", "se", "no", "dk", "gb" }; break;
            }

                foreach (string Country in (string[])prioritet)
                {
                    selectedCountry = Country;
                    var tmpSel = sRetVal.Find(FindCurrentSelected);
                    sRetVal.RemoveAt(sRetVal.FindIndex(FindCurrentSelected));
                    sRetVal.Insert(0, tmpSel);
                }
                //sRetVal[10].Selected = true;
            HttpContext.Current.Cache[CachKey] = sRetVal;
        }
        else
        {
            sRetVal = (List<SelectListItem>)HttpContext.Current.Cache[CachKey];
        }
        return (IEnumerable<SelectListItem>) sRetVal;
    }

都嘗試過:

@Html.DropDownListFor(model => model.Country, new SelectList(CommonHelpers.CountrySelectList(), "Value", "Text", Model.Country), "---Select---")

@Html.DropDownListFor(model => model.Country, CommonHelpers.CountrySelectList(), "---Select---")

有想法嗎?

要求模型屬性(國家/地區)和在SelectList中顯式傳遞的名稱具有不同的名稱,以連接所選項目。 如果沒有提供選定的值,則瀏覽器默認為SelectList中的第一個元素。 這是DropDownList幫助器的已知限制。 我正在完成DDL教程。 您可以在http://code.msdn.microsoft.com/Using-the-DropDownList-67f9367d中獲取完整的代碼。給我發送電子郵件,我將向您發送該教程。Rick.Anderson[at] microsoft.com

您需要將“國家/地區”值傳遞給您的方法,然后檢查所選值

public static IEnumerable<SelectListItem> CountrySelectList(string countryId)
{
    var sRetVal = new List<SelectListItem>();
    // .......................         
    foreach (XmlNode Node in Doc.SelectNodes(String.Format("//Countries/language[@iso=\"{0}\"]/country", GetCulture())))
    {
        var tmpSelect = new SelectListItem();
        tmpSelect.Value = Node.Attributes["iso2"].InnerText.ToString();
        tmpSelect.Text = Node.Attributes["name"].InnerText;
        // Check for selected value
        tmpSelect.Selected = string.Equals(tmpSelect.Value, countryId);
        sRetVal.Add(tmpSelect);
    }
    //...................
    return (IEnumerable<SelectListItem>)sRetVal;
}

或只是讓新的幫手為您做這件事。 像這樣:

public static IEnumerable<SelectListItem> SetSelected(this IEnumerable<SelectListItem> selectList, string selectedValue)
{
    var newList = new List<SelectListItem>();
    var oldList = selectList.ToList();
    for (var i = 0; i < oldList.Count; i++)
    {
        var item = oldList[i];
        item.Selected = string.Equals(item.Value, selectedValue, StringComparison.CurrentCultureIgnoreCase);
        newList.Insert(i, item);
    }
    return newList;
}

這是用法:

@Html.DropDownListFor(model => model.Country, CommonHelpers.CountrySelectList().SetSelected(model.Country), "---Select---")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM