zoukankan      html  css  js  c++  java
  • Html.DropDownList

    一、非强类型:

    Controller:
    ViewData["AreId"] = from a in rp.GetArea()
    select new SelectListItem {
    Text=a.AreaName,
    Value=a.AreaId.ToString()
    };
    View:
    @Html.DropDownList("AreId")

    还可以给其加上一个默认选项:@Html.DropDownList("AreId", "请选择");

    二、强类型:

    DropDownListFor常用的是两个参数的重载,第一参数是生成的select的名称,第二个参数是数据,用于将绑定数据源至DropDownListFor

    Modle:

    public class SettingsViewModel
    {
    Repository rp =new Repository();
    public string ListName { get; set; }
    public IEnumerable<SelectListItem> GetSelectList()
    {
    var selectList = rp.GetArea().Select(a => new SelectListItem {
    Text=a.AreaName,
    Value=a.AreaId.ToString()
    });
    return selectList;
    }
    }

    Controller:
    public ActionResult Index()
    {
    return View(new SettingsViewModel());
    }

    View:
    @model Mvc3Applicationtest2.Models.SettingsViewModel
    @Html.DropDownListFor(m=>m.ListName,Model.GetSelectList(),"请选择")

  • 相关阅读:
    课堂测试-文本操作
    异常的总结
    动手动脑实验-异常
    从小工到专家-读后感3
    从小工到专家-读后感2
    从小工到专家-读后感1
    构建之法读书笔记(一)
    2.12日总结
    BaseAdapter的使用
    Activity之Bundle的使用
  • 原文地址:https://www.cnblogs.com/jinhaoObject/p/4644530.html
Copyright © 2011-2022 走看看