redaction redaction - 1 year ago 94 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
End Enum
End NameSpace


Namespace Models
Public Enum TransactieType
End Enum
End NameSpace

so my search box need to be able to filter


and show of my result

look of my pands/index

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


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

@For Each item In Model
@Html.DisplayFor(Function(modelItem) item.Soort)
@Html.DisplayFor(Function(modelItem) item.TransactieType)
@Html.DisplayFor(Function(modelItem) item.Beschrijving)
@Html.DisplayFor(Function(modelItem) item.Locatie)
@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})


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
as I ready the
is to let string convert to all uppercase but this can't work towards my
because this is an

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

So after the answer of

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 Source

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download