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

    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();
    }

  • 相关阅读:
    jQuery火箭图标返回顶部代码
    类库引用EF
    Html.DropDownList
    MVC validation
    MVC @functions
    MVC 扩展方法特点
    Class 实现IDisposing方法
    MVC两个必懂核心
    Asp.net 服务器Application,Session,Cookie,ViewState和Cache区别
    sqlserver log
  • 原文地址:https://www.cnblogs.com/gengaixue/p/2078256.html
Copyright © 2011-2022 走看看