1

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

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

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

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"])
4

1 回答 1

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 = from x in XElement.Load(path).Elements("country") select 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();
    }`
于 2013-08-06T10:56:17.740 回答