วันพุธที่ 8 กุมภาพันธ์ พ.ศ. 2560

Asp.net MVC - Search simple



หน้าตา


หน้า _Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Admin", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Admin")</li>
                    <li>@Html.ActionLink("About", "About", "Admin")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Admin")</li>
                    <li>@Html.ActionLink("จัดการ Categories", "Index", "Categories")</li>
                    <li>@Html.ActionLink("จัดการ Products", "Index", "Products")</li>
                </ul>


                    @using (Html.BeginForm("Index", "Products", FormMethod.Get, new
                    {
                        @class = "navbar-form navbar-left"
                    }))
                    {
                        <div class="form-group">
                            @Html.TextBox("Search", null, new
                            {
                                @class = "form-control",
                                @placeholder = "ค้นสินค้า"
                            })
                        </div>
                        <button type="submit" class="btn btn-default">ค้นหา</button>
                    }

                
                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>





หน้า Products/Index.cshtml




@model IEnumerable<SuaSilverShop_V2.Models.Product>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")

    @using (Html.BeginForm("Index", "Products", FormMethod.Get))
    {
        <label>Filter by category:</label> @Html.DropDownList("Category", "All")
        <input type="submit" value="Filter" />
    <input type="hidden" name="Search" id="Search" value="@ViewBag.Search" />
    }

</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Category.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>
        <th></th>




    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Category.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </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>
}

</table>



ฟังก็ชัน Index ใน ProductsController

 public ActionResult Index(string category , string search)
        {
            var products = db.Products.Include(p => p.Category);

            if( !String.IsNullOrEmpty(category) )
            {
                products = products.Where( p => p.Category.Name == category) ;
            }

            if( !String.IsNullOrEmpty(search) )
            {
                products = products.Where(p => p.Name.Contains(search) ||
                                            p.Description.Contains(search) ||
                                            p.Category.Name.Contains(search));
                ViewBag.Search = search;

            }

            var categories = products.OrderBy(p => p.Category.Name).Select(p => p.Category.Name).Distinct();

            if (!String.IsNullOrEmpty(category))
            {
                products = products.Where(p => p.Category.Name == category);
            }


            ViewBag.Category = new SelectList(categories);

            return View(products.ToList());
        }





ไม่มีความคิดเห็น:

แสดงความคิดเห็น