【问题标题】:adding a search in a MVC在 MVC 中添加搜索
【发布时间】:2015-12-13 06:31:25
【问题描述】:

我正在尝试使用从This Question 获得的多个 LINQ 查询向 MVC 添加过滤器,如果我取消来自品牌功能的参数(我确实让我的下拉列表工作)而且我不确定,因为在表中我需要过滤品牌是一个数字(所以我认为我需要将该参数作为整数传递),但是我收到一个错误

“参数字典包含方法 'System.Web.Mvc.ActionResult Index(System.String, System.String, System.String, System.Nullable`1[System.Int32], Int32)' in 'BDInventario.Controllers.ArticulosController'。可选参数必须是引用类型、可空类型或声明为可选参数。 参数名称:parameters"

下面是我的代码

Function Index(ByVal sortOrder As String, currentFilter As String, ByVal searchString As String, page As Integer?,brand as Integer) As ActionResult

        ViewBag.CurrentSort = sortOrder
        ViewBag.NameSortParm = If(String.IsNullOrEmpty(sortOrder), "name_desc", String.Empty)

        If Not searchString Is Nothing Then
            page = 1
        Else
            searchString = currentFilter
        End If
        ViewBag.CurrentFilter = searchString
        Dim brandList As New List(Of String)
        Dim brandQuery = From a In db.Articulos
                         Join m In db.Articulo_brand On a.brand_id Equals m.brand_id
                         Order By m.desc_brand
                         Select m.desc_brand


        brandList.AddRange(brandQuery.Distinct)

        ViewBag.brand = New SelectList(brandList)



        Dim articulos = From m In db.Articulos Select m

        Select Case sortOrder
            Case "name_desc"
                articulos = articulos.OrderByDescending(Function(s) s.descripcion)
            Case Else
                articulos = articulos.OrderBy(Function(s) s.articulo_id)
        End Select


        If Not String.IsNullOrEmpty(searchString) Then
            articulos = articulos.Where(Function(Inventario_ADO) Inventario_ADO.descripcion.Contains(searchString))

        End If

我认为我的错误在这部分

If Not String.IsNullOrEmpty(brand) Then
            articulos = articulos.Where(Function(m) m.brand_id.Equals(brand))

        End If
        Dim pageSize As Integer = 50
        Dim pageNumber As Integer = If(page, 1)

        Return View(articulos.ToPagedList(pageNumber, pageSize))

    End Function

非常感谢任何帮助或提示

【问题讨论】:

  • 您的参数brand as Integer 必须是Interger 而不是Nothing。如果您收到该错误,那是因为您没有传递正确的值,但您没有展示如何传递该值,因此无法提供帮助。
  • 我想这就是你要问我的关于@<p> Marca: @Html.DropDownList("brand", "All") Buscar: @Html.TextBox("SearchString", TryCast(ViewBag.CurrentFilter, String)) <input type="submit" value="Filtrar" /></p> End Using 或这个.. 我想Page @IIf(Model.PageCount < Model.PageNumber, 0, Model.PageNumber) of @Model.PageCount @Html.PagedListPager(Model, Function(page) Url.Action("Index", New With {page, .sortOrder = ViewBag.CurrentSort, .currentFilter = ViewBag.CurrentFilter}))
  • 您需要将代码放在问题中,而不是在 cmets 中(并且在 @Html.PagedListPager() 中的任何位置都没有为 brand 传递值,因此出现错误

标签: asp.net-mvc vb.net linq asp.net-mvc-5


【解决方案1】:

错误是因为您的品牌参数必须是一个可以为空的整数,就像页面参数一样。

另外,我会在过滤后进行排序。而且你经常使用 ViewBag,为什么不用 ViewModel?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    相关资源
    最近更新 更多