我想根据搜索加载视图中的产品列表。如果用户输入名称或 ID,则应显示产品。我试过了,但我只能做其中之一。
这就是我尝试的方式。
控制器
public class SearchController : Controller
{
private storeEntities db = new storeEntities();
public ActionResult Find()
{
var det = (from d in db.products
select d).ToList();
return View(det);
}
[HttpPost]
public ActionResult Find(int pid)
{
var det = (from d in db.products
where d.id == pid
select d).ToList();
return View(det);
}
}
看法
@model IEnumerable<SearchAppMvc.Models.product>
@{
ViewBag.Title = "Find";
}
<h2>Find</h2>
<div>
@Html.Partial("_SearchPartial")
</div>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
category
</th>
<th>
productName
</th>
<th>
price
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.category)
</td>
<td>
@Html.DisplayFor(modelItem => item.productName)
</td>
<td>
@Html.DisplayFor(modelItem => item.price)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Details", "Details", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}
局部视图
@using (Html.BeginForm( new RouteValueDictionary {{"controller","Search"},{"action","Find"},{"id","pid"}}))
{
@*@Html.Editor("name")*@
@Html.TextBox("pid")
<input type="submit" value="search"/>
}
模型是使用实体框架完成的