[英]How to pass a list of data from one partial view to another partial view, when both partial views are contained in a same parent view in ASP.NET MVC?
我有 2 個局部視圖, HeadphonesFilters
和HeadphonesList
,它們都包含在Index
視圖中,如下所示:
<div class="container">
<div class="row">
<h3 class="alert alert-primary p-2">Headphones</h3>
<div class="col-4">
@Html.Partial("HeadphonesFilters", new { minPrice=0,maxPrice=0})
</div>
<div class="col-8" id="headphones-list">
@Html.Partial("HeadphonesList")
</div>
</div>
</div>
HeadphonesList
部分視圖接受Headphone
類型對象列表並顯示它們。 HeadphonesFilters
分部視圖應將過濾后的耳機列表返回到HeadphonesList
分部視圖。
所有三個視圖都訪問同一個HeadphonesController
。
HeadphonesFilter
局部視圖使用Filter
方法,如下所示:
public PartialViewResult Filter(int minPrice, int maxPrice)
{
var list = service.GetHeadphones()
.Where(x => x.SKU.SKUPrice >= minPrice &&
x.SKU.SKUPrice <= maxPrice)
.ToList();
return PartialView("HeadphonesList", list);
}
此過濾器方法在單獨的頁面中返回過濾后的耳機,而我希望此列表為 go 到HeadphonesList
部分視圖,並且應使用新列表更新該視圖。
有沒有辦法做到這一點?
我認為您應該使用 Ajax 從 controller 獲取數據,以便在不刷新整個頁面的情況下查看
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.