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

本文介绍了在Orchard CMS中如何实现数据实体的筛选和模糊查询。展示了通过Repository获取列表数据以及根据ID和Name进行精确和模糊查询的方法。在MVC的Index页面上添加筛选功能,利用表单和选择框来过滤Genre字段,从而提供更直观的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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)
  {
       var genre = ( from s in db.Movies
                    orderby s.Genre
                    select s.Genre).Distinct();
       ViewBag.MovieGenre = new SelectList(genre);  //给前台准备下拉列表的数据。
       if (! string .IsNullOrEmpty(movieGenre))
       {
           //筛选
           var movies = from s in db.Movies where s.Genre == movieGenre select s;
           return View(movies);
       }
       return View(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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值