首页 > Web开发 > 详细

@Html.DropDownListFor 绑定列表项

时间:2014-03-19 12:06:46      阅读:554      评论:0      收藏:0      [点我收藏+]

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

View:

bubuko.com,布布扣
        <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>
bubuko.com,布布扣


Code:

bubuko.com,布布扣
        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;
        }
bubuko.com,布布扣

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

@Html.DropDownListFor 绑定列表项

原文:http://www.cnblogs.com/qixue/p/3607632.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!