我有view model
个lists
。让我们称之为动物,汽车,房屋和家具。
这些列表包含4种不同类型的预定义搜索结果。在我的view
我正在尝试使用form
和check boxes
创建submit button
(每个复选框代表某个列表)。在提交表单时,我正在尝试将所选列表组合到一个列表中,并在视图中表示它们。我已经能够在控制器中连接其中的几个列表并将它们发送到视图模型(然后发送到视图),但是无法确定从视图中选择哪些列表。
如果需要详细说明代码,我也非常愿意提供此代码:)
所以我的问题是,如何确定哪个复选框与哪个列表相关,如何继续填写连接列表并相应地更新视图?
有关如何进行的任何指示?感谢所有建议。
当前控制器(filterResult
设置为与furtniture和汽车相结合):
public ActionResult Index(ListPage currentPage, int? page)
{
List<ListItem> filterResult = new List<ListItem>();
ListResult allListResult = _searchService.GetPagesByPageType<>();
ListResult carsResult = _searchService.GetPagesByPageType<Cars>();
ListResult furnitureResult = _searchService.GetPagesByPageType<Furniture>();
ListResult housesResult = _searchService.GetPagesByPageType<Houses >();
filterResult = furnitureResult.Items.Concat(carsResult.Items).ToList();
var model = new ListPageViewModel(currentPage)
{
AllPages = allBaseEditorialPagesResult.Items,
TotalMatching = allBaseEditorialPagesResult.TotalMatching,
FilteredPages = filterResult,
TotalFilteredMatching = filterResult.Count()
};
return View(model);
}
总之;我想从视图中选择filterResult填充的结果。
答案 0 :(得分:1)
解决了它:
查看:
@using (Html.BeginForm("Index"))
{
<b>Animals</b>@Html.CheckBox("checkAnimals")
<b>Furniture</b>@Html.CheckBox("checkFurniture")
<b>Cars</b>@Html.CheckBox("checkCars")
<b>Houses</b>@Html.CheckBox("checkHouses")
<input type="submit" tabindex="2" class="btn" value="Apply filter" />
}
控制器:
public ActionResult Index(ListPage currentPage, int? page, bool checkAnimals = false, bool checkFurniture= false, bool checkCars= false, bool checkHouses= false)
{
List<ListItem> filterResult = new List<ListItem>();
ListResult allListResult = _searchService.GetPagesByPageType<>();
ListResult carsResult = _searchService.GetPagesByPageType<Cars>();
ListResult furnitureResult = _searchService.GetPagesByPageType<Furniture>();
ListResult housesResult = _searchService.GetPagesByPageType<Houses >();
ListResult animalsResult = _searchService.GetPagesByPageType<Animals>();
if (checkAnimals)
{
filterResult = filterResult.Concat(animalsResult.Items).ToList();
}
if (checkFurniture)
{
filterResult = filterResult.Concat(furnitureResult .Items).ToList();
}
if (checkCars)
{
filterResult = filterResult.Concat(carsResult.Items).ToList();
}
if (checkHouses)
{
filterResult = filterResult.Concat(housesResult.Items).ToList();
}
var model = new ListPageViewModel(currentPage)
{
AllPages = allBaseEditorialPagesResult.Items,
TotalMatching = allBaseEditorialPagesResult.TotalMatching,
FilteredPages = filterResult,
TotalFilteredMatching = filterResult.Count()
};
return View(model);
}