1

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);
    }
0

1 Answer 1

2

I would search your initial items (before you made the into a list of TextValuePair) then you could do something like

IEnumerable<Item> items = originalItemsList;
switch (source)
{
    case "1": // or whatever this should be
        items = items.Where(x => x.ItemNumber.IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1);
        break;

    case "2": // or whatever this should be
        items = items.Where(x => x.ItemDescription.IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1);
        break;

    case "3": // or whatever this should be
        items = items.Where(x => x.SerialNumber.IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1);
        break;
}


var result1 = items.Select(item => new TextValuePair() { text = item.Text, Value = item.ItemNumber }).ToList();

return Json(result1, JsonRequestBehavior.AllowGet);

If you can't use the initial objects then I would probably do it like this

var result1 = items.Where(item => TextMatches(item.Text, data, source)).ToList();

and then have a method:

private static bool TextMatches(string text, string data, string source)
{
    // you may want to chose a better delimiter if your text description contains a " - "
    string[] textParts = text.Split(new string[] { " - " }, StringSplitOptions.None);

    switch (source)
    {
        case "1": // or whatever this should be
            return textParts[0].IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1;

        case "2": // or whatever this should be
            return textParts[1].IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1;

        case "3": // or whatever this should be
            return textParts[2].IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1;
    }

    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.