0
我有一个函数可以填充ASP.NET MVC中创建的下拉列表。ASP.NET MVC3 Dropdownlist从不选择编辑视图中的值
public static MvcHtmlString countryDropDown(this HtmlHelper helper, string name, string optionLabel, object selectedValue)
{
XmlDocument doc = new XmlDocument();
doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/countries.xml"));
StringBuilder b = new StringBuilder();
b.Append(string.Format("<select name=\"{0}\" id=\"{0}\">", name));
if (!string.IsNullOrEmpty(optionLabel))
b.Append(string.Format("<option value=\"\">{0}</option>", optionLabel));
foreach (XmlNode node in doc.SelectNodes("//country"))
{
string selected = string.Empty;
if (node.Attributes["name"].Value == selectedValue as string)
{
selected = "selected=\"selected\"";
}
b.Append(string.Format("<option value=\"{0}\" {2}>{1}</option>", node.Attributes["name"].Value, node.Attributes["name"].Value, selected));
}
b.Append("</select>");
return MvcHtmlString.Create(b.ToString());
}
我用这个功能在创建和编辑意见:
@Html.countryDropDown("Country"," ", ViewData["Country"])
这表明国家名单完美,但问题是,我也不会选择在编辑页面保存的价值。 如何修改代码以便它可以在编辑页面中选择值。
您是否尝试过单步执行代码使用调试器,看看为什么你如果失败了呢? – 2012-04-18 10:47:44
它在selectedValue参数中获得一个空值。即ViewData [“Country”]发送空。 – hotcoder 2012-04-18 10:49:56
那么这可能是你的问题 – 2012-04-18 10:50:52