redaction redaction - 3 months ago 19
Vb.net Question

How can I make a search filter in MVC5 that can filter on an enum

What's the correct code to make my search box filter my output of my (database,index,data.model?) (don't know how it's called)

I have 4 category's (soort , Transactietype , Beschrijving , Locatie)
How it looks

I was trying the tutorial of mikesdotnetting on how to add "filtering"

but that's not really working out because he is adding a search only towards lastname and firstname which are strings and I have enums too, where I also want to filter

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


and

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


so my search box need to be able to filter

soort(enum),TransactieType(enum),beschrijving(string),locatie(string)

and show of my result

look of my 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>


and the look of my pandcontroller(function index)

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


I am new in MVC so if you need other code please tell me what I need to add more.

So what I was trying is this :

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


but this can't work because the use of
s.Soort.ToUpper
as I ready the
.ToUpper
is to let string convert to all uppercase but this can't work towards my
s.Soort
because this is an
enum


so whats the correct code to met it search in : soort(enum),TransactieType(enum),beschrijving(string),locatie(string)

So after the answer of
s.Soort.ToString().ToUpper()
and
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


I have the ability to startup the site in a browser but if I use the filter I get the next error


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.


this point towards

Return View(pand.ToList())

Answer

Try s.Soort.ToString().ToUpper().

This will first convert your enum to a string, and then upper case the string.