【问题标题】:MVC5: Having Trouble with Filtering Results And Pagination TogetherMVC5:同时过滤结果和分页时遇到问题
【发布时间】:2019-04-17 20:09:13
【问题描述】:

我有一个添加了分页的索引视图(列表),它运行良好。我有其他视图(列表),它们有一个 html 搜索框,可以将值传递回控制器并且也可以完美运行。但是,当我尝试将两者都放在同一个视图上时。 .我得到一个空引用错误。

错误信息 值不能为空。 参数名称:值

这是产生错误的行:

var catalogs = supplies.Where(s => s.ItemDescription.Contains(searchString));

这是索引视图上的 asp/html 搜索框

form asp-controller="Movies" asp-action="Index">
<p>
    Search Catalog Files: <input type="text" name="SearchString">
    <input type="submit" value="Filter" />
</p>

这是我的索引控制器,它有分页和搜索字符串

 public ActionResult Index(string searchString)
    {



         var supplies = db.ICS_Supplies.OrderByDescending(g => g.Supplies_ID).ToList();
        var catalogs = supplies.Where(s => s.ItemDescription.Contains(searchString));
        var model = new PagedList<ICS_Supplies>(catalogs, 1, 10);

        if (!String.IsNullOrEmpty(searchString))
         {

            catalogs = catalogs.Where(s => s.ItemDescription.Contains(searchString));
         }
        return View(model);

    }

这和异步有什么关系吗?我需要使用以下内容吗?

public async Task<ActionResult> Index(string searchString)

或者我的东西顺序不对?该页面工作正常,如果 SearchString 文本框中有文本。 .但是当页面第一次尝试加载时,它会失败。

任何指针?

【问题讨论】:

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


    【解决方案1】:

    你可以在执行前检查searchString是否为空

    var catalogs = supplies.Where(s => s.ItemDescription.Contains(searchString ?? string.Empty));
    

    【讨论】:

    • 谢谢!那成功了。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多