如何解决在ViewBag数据中搜索TextBox
我正在尝试制作产品比较页面。在类别页面中,当用户单击比较按钮时,该产品即被添加到比较页面。然后,生成相似产品列表并将其传递到视图。如何为类似产品列表创建自动完成搜索框,当用户单击产品时,将显示其中的详细信息和规格。现在,我已经设法填充列表并通过Ajax调用显示类似产品的详细信息,但是我不知道如何显示自动完成的搜索框。我感谢任何建议。
这是进行比较的动作:
public ActionResult Compare(string name)
{
ProductModel productModel;
int id = 0;
//check if product exists
if (!db.Products.Any(x => x.ProductUrl.Equals(name)))
{
return RedirectToAction("Index","Home");
}
productModel = db.Products.Where(x => x.ProductUrl == name).FirstOrDefault();
int CategoryId = db.ProductTemplates.Where(c => c.Id == productModel.ProductTemplateId).Select(i => i.CategoryId).FirstOrDefault();
List<int> SpecGroups = db.CategoryItems.Where(c => c.CategoryId == CategoryId).Select(i => i.SpecGroupId).ToList();
List<SpecItemsModel> SpecItems = db.SpecItems.Where(c => SpecGroups.Contains(c.ParentId)).ToList();
foreach (SpecItemsModel item in SpecItems)
{
ProductSpec specVal = db.ProductSpecs.Where(ps => ps.ProductId == productModel.Id && ps.SpecItemId == item.Id).FirstOrDefault();
if (specVal != null)
{
item.SpecValue = specVal.SpecValue;
}
productModel.Specs.Add(item.LatinName,item);
}
id = productModel.Id;
//similar products
var products = db.Products.AsQueryable();
CategoryModel category = db.Categories.Where(x => x.Id == productModel.ProductTemplate.CategoryId).FirstOrDefault();
if (category != null)
{
int catId = category.Id;
string catName = category.Name;
ViewBag.categoryName = category.Name;
List<SpecItemsModel> options = Products.GetFilter(category.Id);
//initialize the list
//productList = Products.ProductLists(catId);
ViewBag.filters = options;
var childrenIDs = db.Categories.Where(x => x.ParentId == category.Id).Select(x => x.Id);
var grandchildrenIDs = db.Categories.Where(x => childrenIDs.Contains((int)x.ParentId)).Select(x => x.Id);
List<int> catIds = new List<int>();
catIds.Add(category.Id);
catIds.AddRange(childrenIDs);
catIds.AddRange(grandchildrenIDs);
var templates = db.ProductTemplates.Where(t => catIds.Contains(t.CategoryId)).Select(x => x.Id);
products = db.Products.Where(p => templates.Contains(p.ProductTemplateId));
ViewBag.similarProducts = products;
ViewBag.similarProductsList = new SelectList(products,"ShortName","LatinName");
}
//return view with the model
return View(productModel);
}
此操作将生成发现的相似产品的详细信息:
public PartialViewResult SProductSpecs(int id)
{
ProductModel sProduct = db.Products.Where(x => x.Id == id).FirstOrDefault();
//get the specs
int CategoryId = db.ProductTemplates.Where(c => c.Id == sProduct.ProductTemplateId).Select(i => i.CategoryId).FirstOrDefault();
List<int> SpecGroups = db.CategoryItems.Where(c => c.CategoryId == CategoryId).Select(i => i.SpecGroupId).ToList();
List<SpecItemsModel> SpecItems = db.SpecItems.Where(c => SpecGroups.Contains(c.ParentId)).ToList();
foreach (SpecItemsModel item in SpecItems)
{
ProductSpec specVal = db.ProductSpecs.Where(ps => ps.ProductId == sProduct.Id && ps.SpecItemId == item.Id).FirstOrDefault();
if (specVal != null)
{
item.SpecValue = specVal.SpecValue;
}
sProduct.Specs.Add(item.LatinName,item);
}
id = sProduct.Id;
return PartialView("_SProduct",sProduct);
}
在比较视图中,将生成类似产品的列表:
@foreach (ProductModel sProduct in ViewBag.similarProducts)
{
@Ajax.ActionLink(sProduct.ShortName,"SProductSpecs","Home",new { id = sProduct.Id },new AjaxOptions { HttpMethod = "GET",UpdateTargetId = "divSProduct",InsertionMode = InsertionMode.Replace})
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。