我使用MVC4并尝试使用链接进行下拉列表。我写了一个帮手:
public static MvcHtmlString LanguageSelectorDropdown(this System.Web.Mvc.HtmlHelper helper, string name)
{
var culture = Thread.CurrentThread.CurrentCulture;
IList<Culture> data = new CultureRepository().GetEnabled();
var items = new List<SelectListItem>();
items.AddRange(
(from item in data
select new SelectListItem()
{
Text = @"<a href='/Home/Edit/3'>I am a link</a>", //it's only example. Here I run the other method to get dynamic link
Value = item.ID.ToString(),
Selected = culture.Name.Equals(item.ID)
}).ToList());
return new MvcHtmlString(HttpUtility.HtmlDecode(helper.DropDownList(name, items).ToString()));
}
在这个方法的最后,我有:
helper.DropDownList(name, items).ToString() = "<select id=\"LanguageSelector\" name=\"LanguageSelector\"><option value=\"de-DE\"><a href='/Home/Edit/3'>I am a link</a></option>\r\n<option value=\"en-US\"><a href='/Home/Edit/3'>I am a link</a></option>\r\n<option selected=\"selected\" value=\"ru-RU\"><a href='/Home/Edit/3'>I am a link</a></option>\r\n</select>"
HttpUtility.HtmlDecode(helper.DropDownList(name, items).ToString()) = "<select id=\"LanguageSelector\" name=\"LanguageSelector\"><option value=\"de-DE\"><a href='/Home/Edit/3'>I am a link</a></option>\r\n<option value=\"en-US\"><a href='/Home/Edit/3'>I am a link</a></option>\r\n<option selected=\"selected\" value=\"ru-RU\"><a href='/Home/Edit/3'>I am a link</a></option>\r\n</select>"
在结果html页面中:
<select id="LanguageSelector" name="LanguageSelector">
<option value="de-DE">I am a link</option>
<option value="en-US">I am a link</option>
<option value="ru-RU" selected="selected">I am a link</option>
</select>
看起来链接消失了。有没有办法强制它起作用?