在我的搜索控制器中,我有:
public JsonResult Search(string term)
{
var termLower=term.ToLower();
var pictures=_PictureRepo.GetAll();
var productsWereSeached = _ProductRepo.GetAll().Where(x => x.Name.ToLower().Contains(term)).Select(x=> new ProductData
{
Name=x.Name,
Price=x.Price,
Id=x.Id,
Warranty=x.Warranty,
Picture=x.Pictures.FirstOrDefault()
});
ViewBag.NOfMatchedProduct = productsWereSeached.Count();
productsWereSeached = productsWereSeached.Take(2);
foreach (var product in productsWereSeached)
{
product.Picture = _PictureRepo.GetAll().Where(x => x.ProductId == product.Id).FirstOrDefault();
}
return Json(productsWereSeached);
}
在我的 _Layout 我有:
<div>
<input id="nOfMatchedProducts" value='@ViewBag.NOfMatchedProduct'/>
<ul id="realPlaceForSearchItems">
</ul>
</div>
也许我应该把这段代码从_Layout
到PartialView
. 问题是,如何将ViewBag
数据从控制器传递到PartialView
.