【发布时间】:2015-01-09 20:40:29
【问题描述】:
使我的搜索框过滤我的(数据库,索引,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
所以我的搜索框需要能够过滤
soort(enum),TransactieType(enum),beschrijving(string),locatie(string)
并展示我的结果
看看我的盘子/索引
@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
我可以在浏览器中启动网站,但如果我使用过滤器,我会收到下一个错误
EntityFramework.SqlServer.dll 中出现“System.NotSupportedException”类型的异常,但未在用户代码中处理
附加信息:LINQ to Entities 无法识别方法“System.String ToString()”方法,并且该方法无法转换为存储表达式。
这个指向
Return View(pand.ToList())
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
你到底有什么问题?
-
你好,我补充说我走了多远,这可能表明我在这里告诉@John Saunders
标签: asp.net asp.net-mvc vb.net filter enums