带引用的MVC4下拉助手

时间:2014-11-27 13:40:01

标签: asp.net-mvc-4

我使用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\">&lt;a href=&#39;/Home/Edit/3&#39;&gt;I am a link&lt;/a&gt;</option>\r\n<option value=\"en-US\">&lt;a href=&#39;/Home/Edit/3&#39;&gt;I am a link&lt;/a&gt;</option>\r\n<option selected=\"selected\" value=\"ru-RU\">&lt;a href=&#39;/Home/Edit/3&#39;&gt;I am a link&lt;/a&gt;</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>

看起来链接消失了。有没有办法强制它起作用?

0 个答案:

没有答案