mvc列表显示筛选 或者模糊查询

来源:互联网 发布:por una cabeza 知乎 编辑:程序博客网 时间:2024/05/21 11:13

在orchard中获取数据实体

 

public List<nonoRecord> Get()
        {
            return this._nonoRecordRepository.Table.ToList();
        }
        public nonoRecord Get(int id)
        {
            return this._nonoRecordRepository.Fetch(c => c.Id == id).FirstOrDefault();
        }
        public nonoRecord Get(string name)
        {
             return this._nonoRecordRepository.Fetch(c => c.Name == name).FirstOrDefault();         
        }
        public List<nonoRecord> mohuGet(string name)
        {
            return this._nonoRecordRepository.Fetch(c => c.Name.Contains(name)).ToList();      
        }


mvc中 lndex页面加上一些筛选功能。通过给Index传递参数来实现筛选,在页面添加一个selectbox来筛选Genre字段,把MovieController的方法改成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public ViewResult Index(string movieGenre)
 {
      vargenre = (froms in db.Movies
                   orderbys.Genre
                   selects.Genre).Distinct();
      ViewBag.MovieGenre =new SelectList(genre); //给前台准备下拉列表的数据。
      if(!string.IsNullOrEmpty(movieGenre))
      {
          //筛选
          varmovies = froms in db.Movies where s.Genre == movieGenre select s;
          returnView(movies);
      }
      returnView(db.Movies.ToList());
}

在View页面,添加一个Form,里面放上一个选择框和一个按钮,代码如下:

1
2
3
4
@using(Html.BeginForm("Index","Movie", FormMethod.Get))
{
    <p>Genre: @Html.DropDownList("MovieGenre","全部")  <input type="submit" value="筛选" /></p>
}
1
  

效果如下:

image

0 0
原创粉丝点击