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?
Here I recreated your view and added a foreach loop so I could print the output
@model IEnumerable<stack.Controllers.Mobiles>
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<p>
Manufacturer: @Html.DropDownList("searchGenre", "All")
Filter By: @Html.TextBox("SearchString") <br />
<input type="submit" value="Filter" />
</p>
}
@if (Model != null || Model.Any())
{
foreach (var item in Model)
{
<p>@item.ID | @item.Manufacturer | @item.Model | @item.NetworkType </p>
}
}
Now here is the controller. first:
filterresults = filterresults.Where(k=>Manufacturer.Contains(searchString));
you need to do
k => k.Manufacturer.Contains(searchString)
Next I left everything as is so you can follow easily. I just added a Mobiles class so I could recreate your situation. :)
public class Mobiles
{
public int ID { get; set; }
public string Manufacturer { get; set; }
public string Model { get; set; }
public string NetworkType { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index(string searchGenre, string searchString)
{
string item1 = "Manufacturer";
string item2 = "Model";
string item3 = "NetworkType";
var GenreLst = new List<string> { item1, item2, item3, };
List<Mobiles> Mobs = new List<Mobiles>(){
new Mobiles() { ID = 1, Manufacturer = "Samsung", Model = "S4", NetworkType = "Orange" },
new Mobiles(){ ID = 2, Manufacturer = "Nokia", Model ="X1", NetworkType ="O2"},
new Mobiles(){ ID = 3, Manufacturer = "Sony", Model = "Z1", NetworkType = "Orange"}
};
var filterresults = from m in Mobs
select m;
ViewBag.searchGenre = new SelectList(GenreLst);
// ToLower for case
if (!String.IsNullOrEmpty(searchGenre))
{
switch (searchGenre)
{
case "Manufacturer":
filterresults = filterresults.Where(k => k.Manufacturer.Contains(searchString));
break;
case "Model":
filterresults = filterresults.Where(x => x.Model.Contains(searchString));
break;
case "NetworkType":
filterresults = filterresults.Where(v => v.NetworkType.Contains(searchString));
break;
default:
// Something defaulty if you want.
break;
}
}
return View("Index", filterresults);
}
}
Last words: And with that everything works on my end. I would suggest using a ViewModel instead of a view bag, just makes life easier. Then you can store all your form variables in it.
Edited: look here string item1 = "Manufacturer"; string item2 = "Model"; string item3 = "Network Type";
see the Network Type in your GenreLst you have it as "NetworkType", you need to remove the space on item3.
I added a switch structure.. just to make this more readable and better.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.