MVC 3.0 排序 查詢

  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") &nbsp;
            <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>
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章