zoukankan      html  css  js  c++  java
  • @Html.DropDownList 是如何绑定ViewData、ViewBag数据的?

    The DropDownList helper used to create an HTML select list requires a IEnumerable , either explicitly or implicitly. That is, you can pass the IEnumerable explicitly to the DropDownList helper or you can add the IEnumerable to the ViewBag using the same name for the SelectListItem as the model property

    可以传入明确的IEnumerable<SelectListItem>,也可以通过ViewBag或者ViewData隐式地传入,前提是需要相同的名称,比如:

    ViewBag.GenreId或者ViewData["GenreId"]。官方示例:

    public ActionResult SelectCategory() {
    
         List<SelectListItem> items = new List<SelectListItem>();
    
         items.Add(new SelectListItem { Text = "Action", Value = "0"});
    
         items.Add(new SelectListItem { Text = "Drama", Value = "1" });
    
         items.Add(new SelectListItem { Text = "Comedy", Value = "2", Selected = true });
    
         items.Add(new SelectListItem { Text = "Science Fiction", Value = "3" });
    
         ViewBag.MovieType = items;
        ViewData["HourList"] =items ;
    return View(); }

      视图:

    @Html.DropDownList("MovieType")

    以上来自:https://q.cnblogs.com/q/78044/

    不相同名称的处理:

    @Html.Kendo().DropDownListFor(l => l.lb).OptionLabel("Select...").DataTextField("Text").DataValueField("Value").BindTo(ViewData["HourList"] as List<SelectListItem>))
  • 相关阅读:
    day7 反射
    day7 面向对象进阶
    day7 面向对象class()学习
    day6 subprocess模块、logging模块
    day6 hashlib模块
    day6 ConfigParser模块 yaml模块
    day6 xml文件格式的处理
    day6 shelve模块
    day6 SYS模块
    Servlet的学习之Response响应对象(1)
  • 原文地址:https://www.cnblogs.com/djd66/p/15166450.html
Copyright © 2011-2022 走看看