none
asp.net core mvc查询与分页 RRS feed

  • 问题

  • 请问哪位有asp.net core mvc多条件查询与分页的通用方法,麻烦提示一下,自己试了都不太好用,谢谢。
    2017年4月16日 9:04

全部回复

  • 你好,

    你可以参考asp.net core MVC官网教程,代码如下:

    public async Task<IActionResult> Index( string sortOrder, string currentFilter, string searchString, int? page) { ViewData["CurrentSort"] = sortOrder; ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date"; if (searchString != null) { page = 1; } else { searchString = currentFilter; } ViewData["CurrentFilter"] = searchString; var students = from s in _context.Students select s;

    //根据条件筛选数据 if (!String.IsNullOrEmpty(searchString)) { students = students.Where(s => s.LastName.Contains(searchString) || s.FirstMidName.Contains(searchString)); } switch (sortOrder) { case "name_desc": students = students.OrderByDescending(s => s.LastName); break; case "Date": students = students.OrderBy(s => s.EnrollmentDate); break; case "date_desc": students = students.OrderByDescending(s => s.EnrollmentDate); break; default: students = students.OrderBy(s => s.LastName); break; } //使用PaginateList 实现分页 int pageSize = 3; return View(await PaginatedList<Student>.CreateAsync(students.AsNoTracking(), page ?? 1, pageSize)); }

    更多详细信息,请参考:

    https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page#add-a-search-box-to-the-students-index-page

    2017年4月17日 7:58
    版主
  • 谢谢,以前按照这个做过,但通用性较不好。怎么才能做成如MVC5时Pagelist那样的类,比如ViewModels有个如下的类:

    using System.Collections.Generic;

    namespace wzgl.Models.ViewModels
    {
        public class SearchWzxx
        {
            public string SWzmc { get; set; }
            public string SThgg { get; set; }
            public IEnumerable<Wzxx> Wzxx { get; set; }
        }
    }

    按SWzmc和SThgg两个条件的查询结果,怎么把Wzxx传递给一个类来进行分页?

    谢谢您百忙之中给予了答复。

    2017年4月17日 11:49
  • 查询方法如下:

    public async Task<IActionResult> Search(SearchWzxx searchwzxx)
            {
                searchwzxx.Wzxx = await _context.Wzxxs.ToListAsync();
                if (!string.IsNullOrEmpty(searchwzxx.SWzmc))
                {
                    searchwzxx.Wzxx = searchwzxx.Wzxx.Where(w => w.Wzmc.Contains(searchwzxx.SWzmc.Trim()));
                }
                if (!string.IsNullOrEmpty(searchwzxx.SThgg))
                {
                    searchwzxx.Wzxx = searchwzxx.Wzxx.Where(w => w.Thgg.Contains(searchwzxx.SThgg.Trim()));
                }
                return View(searchwzxx.Wzxx);
            }

    2017年4月17日 11:51
  • 你好,

    你可以参考下边的代码: 将ViewModel传递到视图

            public IActionResult Index(TestViewModel testmodel)
            {
                TestViewModel test = new TestViewModel();
                List<ItemModel> itemlist = GetItems();
    
                if (testmodel.SearchString1 != null)
                    itemlist = itemlist.Where(c => c.ID == testmodel.SearchString1).ToList();
                if (testmodel.SearchString2 != null)
                    itemlist = itemlist.Where(c => c.Name == testmodel.SearchString2).ToList();
                test.ItemList = itemlist;
                
                return View(test);
            }
            public List<ItemModel> GetItems()
            {
                List<ItemModel> itemlist = new List<ItemModel>();
                itemlist.Add(new ItemModel() { ID = "1001", Name = "AAA" });
                itemlist.Add(new ItemModel() { ID = "1002", Name = "BBB" });
                itemlist.Add(new ItemModel() { ID = "1001", Name = "CCC" });
                itemlist.Add(new ItemModel() { ID = "1002", Name = "DDD" });
                itemlist.Add(new ItemModel() { ID = "1001", Name = "EEE" });
                return itemlist;
            }

    视图中的代码:

    @model NetCoreTest.Controllers.TestViewModel
    
    @{
        ViewData["Title"] = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Index</h2>
    
    <form asp-action="Index">
        <div class="form-horizontal">
            <div class="form-group">
                <label asp-for="SearchString1" class="col-md-2 control-label"></label>
                <div class="col-md-3">
                    <input asp-for="SearchString1" class="form-control" />
                </div>
                <label asp-for="SearchString2" class="col-md-2 control-label"></label>
                <div class="col-md-3">
                    <input asp-for="SearchString2" class="form-control" />
                </div>
                <div class="col-md-2">
                    <input type="submit" value="Search" class="btn btn-default" />
                </div>
    
            </div>
            <div class="form-group">
                <table class="table">
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                    </tr>
                    @foreach (var item in Model.ItemList)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.ID)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Name)
                            </td>
                        </tr>
                    }
                </table>
    
            </div>
        </div>
        
    </form>
    
    <div>
        <a asp-action="Index">Back to List</a>
    </div>
    

    类:

        public class ItemModel
        {
            public string ID { get; set; }
            public string Name { get; set; }
        }
        public class TestViewModel
        {
            public string SearchString1 { get; set; } //
            public string SearchString2 { get; set; }
            public IEnumerable<ItemModel> ItemList { get; set; }
        }


    2017年5月3日 3:10
    版主