在mvc3中创建相同下拉列表的最佳方法是什么? 正如你在这个链接中看到的那样 Html JS example
我试着这样做
模型:
public enum oporetor
{
greater_then = '>',
less_than = '<',
equal = '='
}
public oporetor getOp { get; set; }
}
查看模型:
@Html.DropDownListFor(model => model.getOp, new SelectList(Enum.GetValues(typeof(Fnx.Esb.ServiceMonitor.ViewModel.AdvanceSearchModel.oporetor))))
视图:
@Html.EditorFor(x => x.AdvanceSearchModel)
我得到了一个基本的下拉列表,其中包含greater_then,less_than,equal。如何在下拉列表中显示&lt;,&gt;,=?
答案 0 :(得分:1)
@Html.DropDownListFor(
model => model.getOp,
((Fnx.Esb.ServiceMonitor.ViewModel.AdvanceSearchModel.oporetor[])Enum.GetValues(
typeof(Fnx.Esb.ServiceMonitor.ViewModel.AdvanceSearchModel.oporetor)
)).Select(x => new SelectListItem
{
Value = x.ToString(),
Text = ((char)x).ToString()
})
)
或更好的方法是直接在您的视图模型中准备这些数据:
public enum Operator
{
greater_then = '>',
less_than = '<',
equal = '='
}
public class AdvanceSearchModel
{
public IEnumerable<SelectListItem> Operators
{
get
{
return ((Operator[])Enum.GetValues(typeof(Operator)))
.Select(x => new SelectListItem
{
Value = x.ToString(),
Text = ((char)x).ToString()
});
}
}
public Operator GetOp { get; set; }
}
然后在视图中简单地说:
@Html.DropDownListFor(model => model.GetOp, Model.Operators)