Controller:
RingRepository ringRepository = new RingRepository();
public ActionResult Index(string sort, string SearchString)
{
ViewBag.NameSortParm = string.IsNullOrEmpty(sort) ? "Name desc" : "";
var list = ringRepository.GetAll();
//search RingName by searchParm
if (!string.IsNullOrEmpty(SearchString))
{
list = list.Where(r => r.ringName.ToUpper().Contains(SearchString.ToUpper()));
}
switch (sort)
{
case "Name desc":
list = list.OrderBy(r => r.ringName);
break;
default:
list = list.OrderByDescending(r => r.ringName);
break;
}
return View(list);
}
View:
@model IEnumerable<LisknoveShop.Models.Ring>
<p>
@using (Html.BeginForm())
{
<p>
Find by RingName: @Html.TextBox("SearchString")
<input type="submit" value="Search" /></p>
}</p>
<table>
<tr>
<th>@Html.ActionLink("RingName", "Index", "Ring", new { area = "Admin", sort = ViewBag.NameSortParm }, null)
</th>
<th>
ringImage
</th>
<th>
price
</th>
<th>
genreId
</th>
<th>
materialId
</th>
<th>
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ringName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ringImage)
</td>
<td>
@Html.DisplayFor(modelItem => item.price)
</td>
<td>
@Html.DisplayFor(modelItem => item.genreId)
</td>
<td>
@Html.DisplayFor(modelItem => item.materialId)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ringId }) |
@Html.ActionLink("Details", "Details", new { id = item.ringId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ringId })
</td>
</tr>
}
</table> mvc