zoukankan      html  css  js  c++  java
  • Html.DropDownList()的用法

    Html.DropDownList()赋默认值:

    页面代码如下:

      <% 
                    List<SelectListItem> list = new List<SelectListItem> {

                    new SelectListItem { Text = "启用", Value = "0",Selected = true},

                    new SelectListItem { Text = "禁用", Value = "1" } };
      %>//list储存dropdownlist的默认值
     <%=Html.DropDownList("state",list,Model.state) %>  //state为实体的属性,默认选中"启用"

    Html.DropDownList()从数据库读取值:

    页面代码如下:

    <%= Html.DropDownList("Category", ViewData["Categories"] as SelectList,"--请选择--",new { @class = "my-select-css-class" } )%>

    Controllers代码:

    public ActionResult Create() 

    {
            List<Category> categories = categoryService.GetAll();
            ViewData["Categories"] = new SelectList(categories, "Id", "Name");
            return View();
    }

    下面是我自己的代码:

    页面代码如下:

    <!--数据库提取数据 -->

    <p>产品分类:<%=Html.DropDownList("category")%></p>

    <!--手动设置数据-->

         <p>状态:<%=Html.DropDownList("status")%></p>

    Controllers代码:

    ViewData["category"] = new SelectList(CategoryManager.GetList(),"id","name"); List<SelectListItem> list = new List<SelectListItem> { new SelectListItem { Text = "启用", Value = "0",Selected = true}, new SelectListItem { Text = "禁用", Value = "1" } }; ViewData["status"] = list;

  • 相关阅读:
    个人项目 源程序特征统计程序(C++)
    自我介绍+软工五问
    团队作业3——需求改进&系统设计
    Four Fundamental Operations(JS) --结对项目
    WordCount of Software Engineering
    代码开发、测试发布
    需求改进---系统设计
    综合系统开发---需求分析
    读书笔记---软件设计原则、设计模式
    自我介绍+课程六问
  • 原文地址:https://www.cnblogs.com/zhuawang/p/2369201.html
Copyright © 2011-2022 走看看