zoukankan      html  css  js  c++  java
  • MVC视图中下拉框的使用

    一、一般变量或对象的绑定

    首先要在controller 中将选项设置成 selecList对象,并赋值给viewBag动态对象。

    public ActionResult Index(string movieGenre,string searchString)
    {
    var GenreLst = new List<string>();
    var GenreQry = from d in db.Movies
    orderby d.Genre
    select d.Genre;
    GenreLst.AddRange(GenreQry.Distinct());
    ViewBag.movieGenre = new SelectList(GenreLst);

    var movies = from m in db.Movies
    select m;
    if (!string.IsNullOrWhiteSpace(searchString))
    {
    movies = movies.Where(s => s.Title.Contains(searchString));

    }

    if (!string.IsNullOrWhiteSpace(movieGenre))
    {
    movies = movies.Where(x => x.Genre == movieGenre);

    }
    return View(movies.ToList());
    }

    在View中有两种用法 

    1、@Html.DropDownList("movieGenre", "All")   name=movieGenre,  自动读取viewBag.movieGenre 数据,也就是说会自动查找ViewBag对象的IEnumerable<SelectListItem >值。第二个参数下拉框是第一行显示的数据。

    或者@Html.DropDownList("movieGenre",null,new { @class="form-control"})

    2、 @Html.DropDownList("movieGenre", ViewBag.movieGenre as SelectList,"全部", new { @class="form-control"})

    @using (Html.BeginForm("Index", "Movie", FormMethod.Get, htmlAttributes: new { @class = "form-inline", role = "form" }))
    {
    <div class="form-group">
    <label class="control-label" for="movieGenre">类别:</label>
    </div>
    <div class="form-group">
    @Html.DropDownList("movieGenre", ViewBag.movieGenre as SelectList,"全部", new { @class="form-control"})
    </div>
    <div class="form-group">
    <label for="searchString" class="control-label">&nbsp;&nbsp;片名:</label>
    </div>
    <div class="form-group">
    @Html.TextBox("searchString", "", htmlAttributes: new { @class = "form-control", placeholder = "请输入片名" })
    </div>
    <input type="submit" value="查找" class="btn btn-primary" />
    }

    截图如下:

     二、 如果是bool类型变量的绑定到下拉框,也可以设置成下拉框的形式,选项分别为 全部,为真,为假。

    控制器:

     public async Task<ActionResult> Index(string departmentID,string userRealName,bool? inCollege)

    {

    if (inCollege != null)
    {
    _users = _users.Where(u => u.InCollege == inCollege);
    }


    List<SelectListItem> listItem = new List<SelectListItem>();
    listItem.Add(new SelectListItem { Value = true.ToString(), Text ="校内" });
    listItem.Add(new SelectListItem { Value = false.ToString(), Text = "校外" });
    ViewBag.InCollegeList = new SelectList(listItem, "Value", "Text");
    ViewBag.CurrentinCollege = inCollege;

     }

    视图:

    <text>&nbsp;&nbsp;</text>

    <label for="inCollege" class="control-label">校内或者校外:</label>
    <div class="form-group">
    @Html.DropDownList("inCollege", ViewBag.InCollegeList as SelectList, "全部", new { @class = "form-control" })
    </div>

    三、也可以直接在视图中指定bool值。

    @Html.DorpdownListFor(x =>x.inCollege,new[] {new SelectListItem{ Value=bool.TureString,Text="校内"},

    new SelectListItem{Value=bool.FalseString,Text="校外"}

    },"请选择")

    四、也可以将枚举类型的绑定到下拉框中。

    将枚举类型的值字符串作为传递参数,

    控制器:

    public ActionResult Index(string courseID, string resourceArrtibute, string searchTitle)
    {
    List<string> _resourceAttributeList = new List<string>();
    _resourceAttributeList.AddRange(Enum.GetNames(typeof(ResourceArrtibute))); //通过Enum.GetNames(typeof(枚举类型))的值转换为字符串,并添加到List 泛型对象。
    ViewBag.ResourceArrtibute = new SelectList(_resourceAttributeList, resourceArrtibute);  //返回所有枚举类型的值,并设定默认值。

    ViewBag.SearchTitle = searchTitle;


    List<Course> courses = new List<Course>();
    var courseListQuery = from r in _resourceService.FindAll().Include(r =>r.Course)
    select r.Course;
    courses.AddRange(courseListQuery.Distinct().OrderBy(c =>c.CourseName));

    ViewBag.CourseID = new SelectList(courses, "CourseID", "CourseName", courseID);
    var resources = _resourceService.FindAll().Include(r => r.Course);
    if(!string.IsNullOrEmpty(courseID))
    {
    resources = resources.Where(r => r.CourseID == courseID);

    }

    if (!string.IsNullOrEmpty(resourceArrtibute))
    {
    resources = resources.Where(r => r.ResourceArrtibute.ToString() == resourceArrtibute); //利用枚举类型的值的ToString()方法将枚举实例值转换为等效的字符串形式。

    }

    if (!string.IsNullOrEmpty(searchTitle))
    {
    resources = resources.Where(r => r.ResourceName.Contains(searchTitle));

    }

    return View(resources.ToList());
    }

    视图:

    @using (Html.BeginForm("Index", "Resource", FormMethod.Get, new { @class = "form-inline", role = "form" }))
    {
    <label for="CourseID" class="control-label">课程名称:</label>
    <div class="form-group">
    @Html.DropDownList("CourseID",null,"全部课程", new { @class="form-control" })
    </div>

    <label for="ResourceArrtibute" class="control-label">资源类型:</label>
    <div class="form-group">
    @Html.DropDownList("ResourceArrtibute", null, "全部类型", htmlAttributes: new { @class = "form-control" })  //将字符串形式的selectlist对象绑定到下拉菜单上。
    </div>

    <label for="searchTextbox" class="control-label"> 通过资源标题查找:</label>
    <div class="form-group">
    @Html.TextBox("SearchTitle", ViewBag.SearchTitle as string, new { @class = "form-control" })
    </div>

    <input type="submit" value="查找" class="btn btn-primary" />


    }

    枚举类型的变量绑定详细可参见博客园另一篇文章

    http://www.cnblogs.com/liuyuanhao/p/4510382.html

  • 相关阅读:
    Nodejs
    webpack与gulp的区别
    gulpjs
    Commonjs、AMD、CMD
    建造者模式
    工厂模式
    设计模式分类
    python的接口
    Python代码教你批量将PDF转为Word
    什么是“堆”,"栈","堆栈","队列",它们的区别?
  • 原文地址:https://www.cnblogs.com/liuyuanhao/p/4317712.html
Copyright © 2011-2022 走看看