I have this MVC page where I have three different "input" elements, all the same class, using the jQuery autocomplete. On any one of them I am doing this in the controller:
[HttpGet]
public ActionResult GetAllItemsEdit(string data, string source)
{
List<TextValuePair> items = InventoryControlRepository.GetAllItemsTVP();
var result1 = items.Where(item => item.Text.Contains(data.ToUpper())).ToList();
return Json(result1, JsonRequestBehavior.AllowGet);
}
The items contain this:
TextValuePair tvp = new TextValuePair();
tvp.Text = item.ItemNumber + " - " + item.ItemDescription + " - SN: " + item.SerialNumber;
tvp.Value = item.ItemNumber;
list.Add(tvp);
So the text in the Text portion of the TVP has to be matched according to the source input field. Any idea how to do this? I would need to split up the item.Text field somehow and check for one of the essentially three columns of data to match the input from the page.
Thanks to Pete's answer I was able to get it done. I had to add a field to the model, essentially a search field which contains the values I wanted for the Text field.
[HttpGet]
public ActionResult GetAllItemsEdit(string data, string source)
{
IEnumerable<ItemModel> models = InventoryControlRepository.GetAllItems();
switch (source)
{
case "txtFindSerial":
models = models.Where(x => x.SerialNumber.IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1);
break;
case "txtFindItem":
models = models.Where(x => x.ItemNumber.IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1);
break;
case "txtFindDescription":
models = models.Where(x => x.ItemDescription.IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1);
break;
}
var result1 = models.Select(item => new TextValuePair() { Text = item.SearchField, Value = item.ItemNumber }).ToList();
return Json(result1, JsonRequestBehavior.AllowGet);
}