如何在MVC5中创建可以过滤枚举的搜索过滤器

时间:2015-01-09 20:40:30

标签: asp.net asp.net-mvc vb.net filter enums

使我的搜索框过滤我的输出(数据库,索引,data.model?)的正确代码是什么(不知道它是如何被调用的)
我有4个类别(soort,Transactietype,Beschrijving,Locatie) How it looks

我正在尝试mikesdotnetting on how to add "filtering"

的教程

但是这并没有真正解决,因为他只是在lastname和firstname上添加搜索字符串,我也有枚举,我也想过滤

Namespace Models
    Public Enum Soort
        Villa
        Kasteel
        GolfVilla
        LuxeAppartement
        Residentie
    End Enum
End NameSpace

Namespace Models
    Public Enum TransactieType
        Niets
        TeHuur
        TeKoop
        Beiden
    End Enum
End NameSpace

所以我的搜索框需要能够过滤
soort(enum),TransactieType(enum),beschrijving(string),locatie(string)

并显示我的结果

看看我的pands / index

@ModelType IEnumerable(Of Exclimmo.Models.Pand)
@Code
ViewData("Title") = "Index"
End Code

<h2>Index</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
@Using Html.BeginForm()
@<p>
    Find by Soort or TransactieType: @Html.TextBox("SearchString")
    <input type="submit" value="Search" />
</p>
End Using
<table class="table">
<tr>
    <th>
        @Html.ActionLink("Soort", "Index", New With {.sortOrder =  ViewBag.soortSortParm})
    </th>
    <th>
        @Html.ActionLink("TransactieType", "Index", New With {.sortOrder = ViewBag.TransactieTypeSortParm})
    </th>
    <th>
       Beschrijving
    </th>
    <th>
        Locatie
    </th>
    <th></th>
</tr>

@For Each item In Model
    @<tr>
        <td>
            @Html.DisplayFor(Function(modelItem) item.Soort)
        </td>
        <td>
            @Html.DisplayFor(Function(modelItem) item.TransactieType)
        </td>
        <td>
            @Html.DisplayFor(Function(modelItem) item.Beschrijving)
        </td>
        <td>
            @Html.DisplayFor(Function(modelItem) item.Locatie)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", New With {.id = item.Id}) |
            @Html.ActionLink("Details", "Details", New With {.id = item.Id}) |
            @Html.ActionLink("Delete", "Delete", New With {.id = item.Id})
        </td>
    </tr>
Next

</table>

和我的pandcontroller(功能索引)的外观

Function Index(ByVal sortOrder As String) As ActionResult
    ViewBag.soortSortParm = If(String.IsNullOrEmpty(sortOrder), "soort_desc", String.Empty)
    ViewBag.TransactieTypeSortParm = If(sortOrder = "TransactieType", "TransactieType_desc", "TransactieType")

    Dim pand = From s In db.Panden Select s

    Select Case sortOrder
        Case "soort_desc"
            pand = pand.OrderByDescending(Function(s) s.Soort)
        Case "TransactieType"
            pand = pand.OrderBy(Function(s) s.TransactieType)
        Case "TransactieType_desc"
            pand = pand.OrderByDescending(Function(s) s.TransactieType)
        Case Else
            pand = pand.OrderBy(Function(s) s.Soort)
    End Select
    Return View(pand.ToList())
End Function

我是MVC的新手,所以如果你需要其他代码,请告诉我需要添加更多代码。

所以我正在尝试的是:

If Not String.IsNullOrEmpty(searchString) Then
    pand = pand.Where(Function(s) s.Soort.ToUpper().Contains(searchString.ToUpper()) _
                                  Or s.TransactieType.ToUpper().Contains(searchString.ToUpper()))
End If

但这不起作用,因为我准备s.Soort.ToUpper时使用.ToUpper就是让字符串转换为全部大写,但这不能对我{{1}起作用因为这是s.Soort

所以你遇到的正确代码是什么?搜索:soort(enum),TransactieType(enum),beschrijving(string),locatie(string)

enums.Soort.ToString().ToUpper()

的答案之后
s.TransactieType.ToString().ToUpper()

我有能力在浏览器中启动网站,但如果我使用过滤器,我会收到下一个错误

  

类型&#39; System.NotSupportedException&#39;的例外情况发生在EntityFramework.SqlServer.dll中但未在用户代码中处理

     

其他信息:LINQ to Entities无法识别方法&#39; System.String ToString()&#39;方法,并且此方法无法转换为商店表达式。

这一点

If Not String.IsNullOrEmpty(searchString) Then
    pand = pand.Where(Function(s) s.Soort.ToString().ToUpper().Contains(searchString.ToUpper()) _
                                          Or s.TransactieType.ToString().ToUpper().Contains(searchString.ToUpper()))
End If

1 个答案:

答案 0 :(得分:0)

尝试s.Soort.ToString().ToUpper()

这会首先将您的enum转换为字符串,然后将大写字母转换为字符串。