我有一个这样的对象:
public class AppointmentStatus
{
public int Id {get;set;}
public string I18NKey {get;set;}
}
I18NKey是指翻译的关键。
在我的表单中,我使用选择列表创建一个下拉列表:
Html.DropDownListFor(x=>x.Id, new SelectList(MyListOfStatus, "Id","I18NKey")
有了这个我检索键的值作为文本,我想编辑每个SelectListItem中的属性文本。
我使用过这样的东西:
public static SelectList TranslateValue(SelectList list)
{
foreach (var tmp in list)
{
tmp.Text = I18nHelper.Message(tmp.Text);
}
return list;
}
但它什么都没改变! Text属性仍然相同,为什么?
答案 0 :(得分:0)
您需要在下拉列表中更改绑定:
Html.DropDownListFor(x => x.Id,
TranslateValue(new SelectList(MyListOfStatus, "Id","I18NKey"))
同时检查I18nHelper.Message
是否按预期工作。
答案 1 :(得分:0)
我终于找到了解决方案:SelectList似乎是基于它需要的IEnumerable。 每个SelectListItem的属性Text都是 dynamics ,并且取决于IEnumerable的每个项目。
我们需要更改 IEnumerable ,而不是 SelectList :
我最终做了什么:
public class AppointmentStatus
{
public int Id {get;set;}
public string I18NKey {get;set;}
public string Translation {get;set;}
}
public static IEnumerable<AppointmentStatus> TranslateValue(IEnumerable<AppointmentStatus> list)
{
foreach (var tmp in list)
{
tmp.Translation = I18nHelper.Message(tmp.I18NKey);
}
return list;
}
Html.DropDownListFor(x=>x.Id, new SelectList(HelperClass.TranslateValue(MyListOfStatus), "Id","Translation")
希望它会对你有所帮助。