如何在Mvc中显示应用程序的每页中的搜索框
How to display Search Box in Every Page of the application in Mvc
我想在应用程序的每个页面中显示搜索框。我尝试在布局中使用"渲染为局部视图"。但它不起作用。请任何机构建议
/搜索控制器代码:/
public class SearchController : Controller
{
// Filtering the Search with product name
public ActionResult SearchProduct(string SearchString)
{
FlipcartDBContext db = new FlipcartDBContext();
var products = from p in db.Products select p;
products = products.Where(s => s.ProductName.StartsWith(SearchString));
return View(products);
}
// Return a Json Result
public JsonResult GetProducts(string term)
{
FlipcartDBContext db = new FlipcartDBContext();
List<string> products;
products = db.Products.Where(s=>s.ProductName.StartsWith(term)).Select(x=>x.ProductName).ToList();
return Json(products, JsonRequestBehavior.AllowGet);
}
}
/SearchProduct.cshtml:/
jQuery(function ($) {
$("#SearchString").autocomplete({
source: '@Url.Action("GetProducts","Search")',
minLength: 2,
});
});
@using (Html.BeginForm())
{
<p>
@Html.TextBox("SearchString", null, new { id = "SearchString" }) <br />
<input type="submit" value="Search" id="Search" />
</p>
}
/*显示产品*/
@foreach(var item in Model)
{
<table>
<tr>
<td>/* Displaying Selected Product Detail */
@Html.DisplayFor(modelItem => item.ProductName)
</td>
<td>
<a href="@Url.Action("Details","Store",new{id=item.PkProductID})">here</a>
</td>
</tr>
</table>.
/请建议,我是jquery和mvc的新手。提前感谢/
将具有一个名为SearchString的属性的视图模型绑定到局部视图更换
@using (Html.BeginForm())
带有
@model viewModel
@using (Html.BeginForm(actionName, contollerName, FormMethod.Post, new { id = id }))
{
@Html.TextBoxFor(model=>model.SearchString)
<input type='submit'/>
}
其中
controllerName是SearchController
actionName是SearchProduct
并且在SearchProduct方法/操作中将SearchString替换为viewModel
相关文章:
- 搜索api在mac上显示对话框
- 谷歌水印未显示在自定义搜索框中
- 使用API在我的网站Google-Map上显示搜索地址的纬度和经度
- 检查搜索结果是否存在多次如果是,则在Javascript中只显示一个结果
- 显示可链接的搜索结果+对齐方式
- 如何获取显示在谷歌搜索右侧的图像链接
- 在谷歌地图上显示所有搜索到的位置结果
- 使自动完成可搜索下拉菜单don'我不想在点击搜索时显示列表
- 为自定义网站创建JavaScript搜索框,创建显示搜索结果的弹出窗口
- 搜索XML文件并使用javascript显示结果
- Google Places API显示雷达搜索中的位置图标/图像
- 一页主题搜索/用下一个按钮突出显示文本
- 自动完成搜索无法正常工作,请不要迭代$中的所有元素.每个ul-li都会及时显示响应
- 使用 jQuery UI 折叠面板显示 couchDB 数据库搜索结果时出现问题
- 不显示 jsTree 搜索文本框
- 我可以配置默认的角度过滤器以显示字符串,以便以搜索词开头的字符串排在第一位
- 如何在 jQuery 中显示“未找到结果”列表搜索
- 显示移动窗口时引导导航栏中的 atach 搜索栏
- 如何隐藏基于先前搜索显示的先前表行
- jQuery智能搜索显示/隐藏内容元素