无法将所选值分配给Html.Dropdownlist MVC 4

时间:2013-06-07 08:09:43

标签: c#-4.0 asp.net-mvc-4

当用户从数据库中检索他的个人资料信息时,我无法使用适当的值绑定下拉列表。

用户的性别是女性。当用户检索按钮单击时的信息时,下拉列表会将默认值加载为“男性”,“女性”,最后是“其他”。但是,我希望它以“女性而不是男性”呈现。这是我的代码。

按钮点击表单以检索详细信息

case "btnGo":
    IntialStage();
    item = onjLoad.ReadLoadPatientData(controlID["FirstrName"].ToString().Replace(",",""));
    ViewData["PatientCharNumber"] = item.ChartNumber;
    break;
}

加载下拉值InitialStage()

 IEnumerable<BindindClass> gender = onjLoad.LoadGenderMaster();
ViewData["GenderMaster"] = from c in gender
                           select new SelectListItem

                           {
                               Text = c.Text,
                               Value = c.Value,
                           };

查看

@Html.DropDownListFor(m => m.Gender, (IEnumerable<SelectListItem>)ViewData["GenderMaster"])

1 个答案:

答案 0 :(得分:1)

按照以下示例,在下拉列表中显示所选值

@Html.DropDownListFor(model => model.Country, (IEnumerable<SelectListItem>)ViewData["CountryList"], new { id = "card_country", @class = "donation-ddlcountry" })

`public void CountryList(string countryCode)         {             string path = Server.MapPath(@“〜\ Country.xml”);             IEnumerable result =来自XElement.Load(path)中的x。元素(“country”)                                            选择x;

        var listItems = new List<SelectListItem>();

        foreach (var xElement in result)
        {
            listItems.Add(new SelectListItem
            {
                Text = xElement.Element("text").Value,
                Value = xElement.Element("value").Value

            });
        }

        if (countryCode == string.Empty)
        {
            var selected = listItems.Where(x => x.Value == "US").First();
            selected.Selected = true;
        }
        else
        {
            var selected = listItems.Where(x => x.Value == countryCode).First();
            selected.Selected = true;
        }

        ViewData["CountryList"] = listItems.ToList();
    }`