Reputation: 167
Is it possible to filter view records via a search for a int? It words but a string but when I try any adapt it to draw all records that match an int I get error: DbComparisonExpression requires arguments with comparable types. in reference to the return view.
Controller:
public ActionResult Index(string sortOrder,string search)
{
ViewBag.TimeSortParam = String.IsNullOrEmpty(sortOrder) ? "Time_desc" : "";
ViewBag.PrioritySortParam = sortOrder == "Priority" ? "Priority_desc" : "";
var incidents = from s in db.Incidents.Include(i => i.Defect).Include(i => i.Operator)
select s;
if (!String.IsNullOrEmpty(search))
{
incidents = incidents.Where(s => s.IncidentID.Equals(search));
}
return View(incidents.ToList());
View
Find by id: @Html.TextBox("Search")
I can see the problem is I am using a string for but I cant find a solution for int
Upvotes: 2
Views: 2940
Reputation: 167
If anyone is interested this worked, although I am not sure its the most efficient way to filter by an int:
public ActionResult Index(string sortOrder, int? search)
{
ViewBag.TimeSortParam = String.IsNullOrEmpty(sortOrder) ? "Time_desc" : "";
ViewBag.PrioritySortParam = sortOrder == "Priority" ? "Priority_desc" : "";
var incidents = from s in db.Incidents.Include(i => i.Defect).Include(i => i.Operator)
select s;
if (search != null)
{
incidents = incidents.Where(s => s.IncidentID == (search));
}}
return View(incidents.ToList());
Upvotes: 1
Reputation: 12448
In your statement, change to:
s.IncidentID==Convert.ToInt32(search)
Upvotes: 0