【发布时间】: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