@Html.DropDownListFor 绑定列表项

MVC中为 DropDownListFor 绑定列表项, 一种方案从后台加载列表内容,通过ViewData传递到前台页面.

View:

@Html.DropDownListFor 绑定列表项
        <div class="editor-label">
            @Html.LabelFor(model => model.CategoryType)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.CategoryType, ViewData["Type"] as SelectList)
            @Html.ValidationMessageFor(model=>model.CategoryType)
        </div>
@Html.DropDownListFor 绑定列表项


Code:

@Html.DropDownListFor 绑定列表项
        public ActionResult Create()
        {
            ViewData["Type"] = GenerateList();
            return View();
        }

        public static SelectList GenerateList()
        {
            List<SelectListItem> items = new List<SelectListItem>()
            {
                new SelectListItem(){Text="Income", Value="income"},
                new SelectListItem(){Text="Outcome", Value="outcome"}
            };

            SelectList generateList = new SelectList(items, "Value", "Text");

            return generateList;
        }
@Html.DropDownListFor 绑定列表项

@Html.DropDownListFor 绑定列表项,布布扣,bubuko.com

@Html.DropDownListFor 绑定列表项

上一篇:Linux proc目录下 几个系统文件下的各项参数 (cpuinfo,uptime,meminfo,stat,loadavg)


下一篇:jquery validation验证身份证号、护照、电话号码、email