ASP.NET MVC 5-值不能为null。参数名称:来源

时间:2019-06-08 20:07:06

标签: c# html asp.net-mvc asp.net-mvc-5

我试图制作一个简单的Web应用程序来显示带有.mdf数据库数据的网格。但是,似乎我将“模型”(源自“源”)返回为null。

HomeController.cs:

public List<BooksTable> GetBooks(string search, string sort, string sortdir, int skip, int pageSize, out int totalRecord){
    using(MyDatabaseEntities dc = new MyDatabaseEntities()){
        var v = (from a in dc.BooksTables
            where 
                a.Author.Contains(search) || 
                a.Title.Contains(search) ||
                a.Year.ToString().Contains(search)
           select a);
        totalRecord = v.Count();
        v = v.OrderBy(sort + " " + sortdir);
        if(pageSize > 0)
            v = v.Skip(skip).Take(pageSize);
        return v.ToList();
    }
}

Index.cshtml:

@model List<LibraryP3.BooksTable>

@{
    Layout = null;
    var grid = new WebGrid(canPage: true, rowsPerPage: 10);
    grid.Bind(source: Model, rowCount: ViewBag.TotalRows, autoSortAndPage:false);
}

这是我在运行它后遇到的错误:System.ArgumentNullException:'值不能为null。参数名称:来源'

[编辑-添加的操作方法]


    public ActionResult Index(int page = 1, string sort = "Author", string sortdir = "asc", string search = ""){
        int pageSize = 10;
        int totalRecord = 0;
        if (page < 1)
            page = 1;
        int skip = (page * pageSize) - pageSize;
        var data = GetBooks(search, sort, sortdir, skip, pageSize, out totalRecord);
        ViewBag.TotalRows = totalRecord;
        ViewBag.search = search;
        return View();
    }

0 个答案:

没有答案