【问题标题】:Setting a variable via dropdownlist in order to limit an index view通过下拉列表设置变量以限制索引视图
【发布时间】:2015-03-06 21:22:13
【问题描述】:

我有一个 Products 控制器,每个产品都包含一个 location 属性。 我希望“主页”,即“主页”控制器视图,显示产品不同位置的下拉列表。选择位置后,我需要产品控制器的索引视图来仅显示该位置的产品。我的设置如下:

'首页索引视图'

@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 索引 & IndexLocation 方法'

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

我的主页显示的dropdownmenu具有适当的选项,但是当我选择一个位置并按Enter键时,它会尝试重定向到产品视图,但我收到了我的产品索引的类别查找错误: "没有具有键 'category' 的 'IEnumerable' 类型的 ViewData 项。"

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

@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 />

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

【问题讨论】:

  • 您是否尝试过调试,如果是,它究竟会在哪里引发错误?
  • 您有许多问题,如果在IndexLocation() location 方法中返回索引视图,但不将模型传递给它,则主要问题会导致错误。目前尚不清楚您采用这种方法的确切原因,但您需要重定向到Index() 方法或在IndexLocation 方法中分配ViewBag.category 属性,以便将category 传递给视图(因此当前为null例外)
  • @StephenMuecke 它最终抛出错误:Category: @Html.DropDownList("category", "All") 我将尝试重定向到 index 方法。对于我正在尝试做的事情,您有不同/更好的解决方案吗?
  • 错误是因为category 为空(您永远不会在IndexLocation() 方法中将category 的值传递给视图,仅在Index() 方法中(当您执行@987654336 @)。你真的应该考虑使用 ajax 来避免所有的重定向。

标签: c# asp.net-mvc razor model-view-controller drop-down-menu


【解决方案1】:

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

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

这意味着要确保您的所有控制器都必须为 ViewBag.category 分配一个选择列表,就像您对 ViewBag.locations 所做的那样,否则您将拥有“没有类型为 'IEnumerable' 的 ViewData 项有键 'category'。"

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

【讨论】:

  • 任何关于使用 Ajax.BeginForm 的帮助或教程链接?不太熟悉如何做到这一点
  • 这篇文章是我用葡萄牙语写的,所以请使用您的浏览器翻译成英文,pt.stackoverflow.com/a/53624/22542,我还能做些什么让您投票支持我对您帖子的回答或勾选收藏夹?跨度>
猜你喜欢
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 2013-06-01
相关资源
最近更新 更多