Here is my existing code :
MobileDatas/Index.cshtml
@using (Html.BeginForm("Index", "MobileDatas", FormMethod.Get))
{
<p>
Manufacturer: @Html.DropDownList("searchGenre", "All")
Filter By: @Html.TextBox("SearchString") <br />
<input type="submit" value="Filter" />
</p>
}
MobileDatasController.cs
public ActionResult Index(string searchGenre, string searchString)
{
string item1 = "Manufacturer";
string item2 = "Model";
string item3 = "Network Type";
var GenreLst = new List<string> { "Manufacturer", "Model", "NetworkType", };
var filterresults = from m in db.MobileDatas
select m;
ViewBag.searchGenre = new SelectList(GenreLst);
if (GenreLst.Contains(Convert.ToString(item1)))
{
if (!string.IsNullOrEmpty(searchString))
{
filterresults = filterresults.Where(k=>Manufacturer.Contains(searchString));
}
}
else if (GenreLst.Contains(Convert.ToString(item2)))
{
if (!string.IsNullOrEmpty(searchString))
{
filterresults = filterresults.Where(x => x.Model.Contains(searchString));
}
}
else if (GenreLst.Contains(Convert.ToString(item3)))
{
if (!string.IsNullOrEmpty(searchString))
{
filterresults = filterresults.Where(v => v.NetworkType.Contains(searchString));
}
}
return View(filterresults);
}
Database: MobileData
MobileID Manufacturer Model NetworkType
1 Samsung S4 Orange
2 Nokia X1 O2
3 Sony Z1 Orange
Hi everyone
I'm having some trouble finding/coming up with a solution to my problem. I'm trying to search my table based on the column selected in DropDownList and return the result
for example: Manufacturer selected + "Sony" typed
the result should just show : 3 Sony Z1 Orange
As it stands my code seams to work for the first if statement if(GenreLst.Contains(Convert.ToString(item1)))
Edit:
found out the above code is breaking as the items are always in GenreList.
How can i return the selected item to compare against item1?
NullOrEmptycheck at the top, one time