在MVC中添加搜索

时间:2015-09-16 15:12:34

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

我试图使用我从This Question获得的多个LINQ查询向MVC添加过滤器,并且我想在此处创建一个类似“添加流派”的过滤器,这是一个下拉列表Tutorial我从品牌的功能中取下参数(我确实让我的下拉列表工作)而事实是我不确定,因为在表中我需要过滤品牌是一个数字(所以我想我需要传递该参数为整数),但我得到一个错误

  

“参数字典包含参数'brand'的非可空类型'System.Int32'的null条目,用于方法'System.Web.Mvc.ActionResult Index(System.String,System.String,System.String, System.Nullable`1 [System.Int32],Int32)'在'BDInventario.Controllers.ArticulosController'中。可选参数必须是引用类型,可空类型,或者声明为可选参数。   参数名称:参数“

以下是我的代码

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

任何帮助或提示都会非常准确

1 个答案:

答案 0 :(得分:0)

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

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