0
votes

I'm brand new to MVC and i'm trying get my dropdown list to dynamically change based on the value a user selects from a list before. The change is just a filter of the list based on the value chosen. I feel like there should be a way to filter my list based on the drop down picked i just am not sure how to do that. It seems like I should be able to write a js function on the view that does something like this:

function 
{
selectedItem()
if x == "ListItemValue"

var CatListItems = new List<SelectListItem>();
    CatListItems.Add(new SelectListItem { Text = String.Empty, Value = String.Empty });
    foreach (var ZCategory in ViewBag.ZCategory)

    if CaTlistItems.CategoryForeignKeyID = 3

    {
        CatListItems.Add(new SelectListItem { Text = ZCategory, Value = ZCategory });
    }
}

Here is my code so far:

Model

public class ArmyRace
{
    [Key]
    public int RaceID { get; set; }
    public string Race { get; set; }
    public virtual ItemCategory ItemCategory { get; set; }
}

public class ItemCategory
{
    [Key]
    public int CategoryID { get; set; }
    public string ArmyCategory { get; set; }
}

Controller:

List<string> Category = new List<string>(2);

var ICategory = from i in db.ItemCategory
                        select i;

foreach (var x in ICategory)
        {
            Category.Add(x.ArmyCategory);
        }
        ViewBag.ZCategory = Category;

View

var CatListItems = new List<SelectListItem>();
CatListItems.Add(new SelectListItem { Text = String.Empty, Value = String.Empty });
foreach (var ZCategory in ViewBag.ZCategory)
{
    CatListItems.Add(new SelectListItem { Text = ZCategory, Value = ZCategory });
}
1
google "jquery cascading dropdown". there are several examples there - Matt Bodily
Is there no way to do it from the selectlistitem that is already added to the page? Again i'm really new but i feel like the information should be there already and therefor there should be some way to get it out. - Myster2
jquery is the way to manipulate data on the view. I usually do an ajax call to get the results back from the controller. If you want to pull from the view you can loop through the model with jquery and pull out a result set. - Matt Bodily
Thanks Matt, I will give it a try. What I'm trying to avoid is having to update the code every time I add a new category to the database. I'll play around and see what is given to the view. - Myster2

1 Answers

0
votes

What I ended up doing for this one is creating 4 seperate lists. One with everything in the dropdown and 3 with the filters i wanted. I then used the simple $jquery.hide() and show functions to show or hide the ones I wanted. Finally i had to change the ID using the $jquery.attr() function for the list that is not active so the POST method would add the correct item to the database.

I believe this is not the ideal way, if I could have somehow brought my lists over with some type of identifier so i could parse through them on the view that would have been ideal but i didn't see a way of doing this.