通过下拉列表设置变量以限制索引视图

Setting a variable via dropdownlist in order to limit an index view

我有一个产品控制器,每个产品都包含一个位置属性。 我想要 'Home Page',所以 'Home' 控制器视图显示产品不同位置的下拉列表。一旦某个位置被 selected,我需要我的产品控制器的索引视图来仅显示该位置的产品。我的设置如下:

'Home Index View'

@using Microsoft.AspNet.Identity
@model ListView.Controllers.AspNetProduct

...

@using (Html.BeginForm("IndexLocation", "Products", FormMethod.Get))
    {
        <p>
            Enter your location: <br />
            @Html.DropDownListFor(model => model.Location, ListView.Controllers.ProductsController.Locations)
            <button type="submit">Submit</button>
        </p>
    }

地点。列表位于 ProductsController

// Location List
    public static List<SelectListItem> Locations = new List<SelectListItem>()
    {
        new SelectListItem() {Text="Beaverton-Hillsboro, OR", Value="Beaverton-Hillsboro, OR"},
        new SelectListItem() {Text="Corvallis, OR", Value="Corvallis, OR"},
        new SelectListItem() {Text="Eugene, OR", Value="Eugene, OR"},
        new SelectListItem() {Text="Medford, OR", Value="Medford, OR"},
        new SelectListItem() {Text="Portland, OR", Value="Portland, OR"},
        new SelectListItem() {Text="Salem, OR", Value="Salem, OR"},
        new SelectListItem() {Text="Tualatin, OR", Value="Tualatin, OR"},
        new SelectListItem() {Text="Vancouver, WA", Value="Vancouver, WA"},
        new SelectListItem() {Text="Wilsonville, OR", Value="Wilsonville, OR"}
    };

'ProductsController Index & IndexLocation Method'

public ActionResult IndexLocation(string location)
    {
        ViewBag.location = location;

        return View("Index");
    }

    // GET: Products
    public ActionResult Index(string category, string searchString)
    {
        string location = ViewBag.location;

        var CategoryLst = new List<string>();

        var CategoryQry = from d in db.AspNetProducts
                          orderby d.Category
                          select d.Category;

        CategoryLst.AddRange(CategoryQry.Distinct());
        ViewBag.category = new SelectList(CategoryLst);

        var aspNetProducts = db.AspNetProducts.Include(a => a.AspNetUser);

        if (!String.IsNullOrEmpty(location))
        {
            aspNetProducts = aspNetProducts.Where(a => a.Location == location);
        }

        if (!String.IsNullOrEmpty(searchString))
        {
            aspNetProducts = aspNetProducts.Where(a => a.Title.Contains(searchString));
        }

        if (!string.IsNullOrEmpty(category))
        {
            aspNetProducts = aspNetProducts.Where(a => a.Category == category);
        }

        return View(aspNetProducts.ToList());
    }

My home page show's the dropdownmenu with the appropriate options, but when I select a location and press enter, it tries redirecting to the Products View, but I get an error with the category lookup for my products指数: “没有 'IEnumerable' 类型的 ViewData 项具有键 'category'。

这是我的产品视图索引的开头,如果我在此页面开始构建,它非常适合搜索,但如果我从主页开始,它就不起作用,select 位置, 然后重定向到此视图..

@using (Html.BeginForm("Index", "Products", FormMethod.Get))
{
    <p>
        Category: @Html.DropDownList("category", "All")
        Title: @Html.TextBox("SearchString")
        <input type="submit" value="Search" /><br />
        @Html.ActionLink("Clear Filters", "Index", "Products")
    </p>
} <br />

感谢任何帮助,因为我被卡住了..!

代码下面的行查找 ViewBag.category :

Category: @Html.DropDownList("category", "All")

这意味着要确保您所有的控制器都必须分配给 ViewBag.category 和 select 列表,就像您对 ViewBag.locations 所做的一样,否则您将拥有 "There is no ViewData item of type 'IEnumerable' that has the key 'category'."

另一个建议是使用 Ajax.BeginForm 在母版页上重新加载部分视图,而无需重定向