0

在 mvc3 中创建相同下拉列表的最佳方法是什么?正如您在此链接中看到的 Html JS 示例

我试着这样做

模型:

   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)

我得到了一个基本的下拉列表,其中包含大于、小于、等于。如何在下拉列表中显示 <,>,=?

4

1 回答 1

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)
于 2012-09-20T11:42:17.153 回答