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