• @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>))
  • 相关阅读:
    Java运算符
    Java数据类型,常量与变量
    内存
    cmd编码
    DELL G7重置电脑操作步骤
    关于es6中...运算符的总结
    js数组与字符串相互转换
    JS中的continue,break,return的区别
    git提交步骤
    数据修改后点击确定没有保存 还是原来的数据
  • 原文地址:https://www.cnblogs.com/djd66/p/15166450.html
Copyright © 2020-2023  润新知