ASP.NET MVC系列:為視圖添加查詢功能


  首先,在MoviesController里添加一個查詢方法,代碼如下

        public ActionResult SearchIndex(string title)
        {
            //查詢數據庫中的電影表
            var movies = from m in db.Movies
                         select m;

            if (!string.IsNullOrEmpty(title))
            {
                //查詢包含title的電影
                movies = movies.Where(m => m.Title.Contains(title));
            }
            return View(movies);
        }

  為SearchIndex方法創建視圖,並選擇視圖的模型類Movie和框架模板List

  添加完成視圖后,我們在瀏覽器中輸入URL地址http://localhost:60534/Movies/searchindex?title=123(我已經在數據庫中添加了title=3的數據),它已經能夠為我們查詢數據

  現在我們在SearchIndex視圖再加上一個查詢按鈕,通過按鈕提交查詢條件

    @using (@Html.BeginForm())
    {
    <p>
        Titile:@Html.TextBox("title")<br />
        <input type="submit" value="Filter" />
    </p>
    }

  一個基本的查詢功能就實現了

  http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM