我怎样才能在 MVC5 中制作一个可以过滤枚举的搜索过滤器
How can I make a search filter in MVC5 that can filter on an enum
让我的搜索框过滤我的(数据库,索引,data.model?)的输出的正确代码是什么(不知道它是如何调用的)
我有 4 个类别(soort、Transactietype、Beschrijving、Locatie)
How it looks
我正在尝试 mikesdotnetting on how to add "filtering"
的教程
但这并没有真正解决,因为他只添加了对姓氏和名字的搜索,它们是字符串,我也有枚举,我也想在其中过滤
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
所以我的搜索框需要能够过滤
排序(枚举),TransactieType(枚举),beschrijving(字符串),locatie(字符串)
并显示我的结果
我的样子 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
但这行不通,因为在我准备 .ToUpper
时使用 s.Soort.ToUpper
是为了让字符串转换为全部大写,但这对我的 s.Soort
不起作用因为这是 enum
所以在 soort(enum),TransactieType(enum),beschrijving(string),locatie(string)
中搜索的正确代码是什么?
所以在 s.Soort.ToString().ToUpper()
和 s.TransactieType.ToString().ToUpper()
的回答之后
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
我可以在浏览器中启动网站,但如果我使用过滤器,我会收到下一个错误
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
这一点指向
Return View(pand.ToList())
尝试s.Soort.ToString().ToUpper()
。
这将首先将您的 enum
转换为字符串,然后 然后 将字符串大写。
让我的搜索框过滤我的(数据库,索引,data.model?)的输出的正确代码是什么(不知道它是如何调用的)
我有 4 个类别(soort、Transactietype、Beschrijving、Locatie)
How it looks
我正在尝试 mikesdotnetting on how to add "filtering"
的教程但这并没有真正解决,因为他只添加了对姓氏和名字的搜索,它们是字符串,我也有枚举,我也想在其中过滤
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
所以我的搜索框需要能够过滤
排序(枚举),TransactieType(枚举),beschrijving(字符串),locatie(字符串)
并显示我的结果
我的样子 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
但这行不通,因为在我准备 .ToUpper
时使用 s.Soort.ToUpper
是为了让字符串转换为全部大写,但这对我的 s.Soort
不起作用因为这是 enum
所以在 soort(enum),TransactieType(enum),beschrijving(string),locatie(string)
中搜索的正确代码是什么?所以在 s.Soort.ToString().ToUpper()
和 s.TransactieType.ToString().ToUpper()
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
我可以在浏览器中启动网站,但如果我使用过滤器,我会收到下一个错误
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
这一点指向
Return View(pand.ToList())
尝试s.Soort.ToString().ToUpper()
。
这将首先将您的 enum
转换为字符串,然后 然后 将字符串大写。