none
[ASP.NET MVC] 如何呈現(關鍵字)搜尋結果? RRS feed

  • 問題

  • 首先,對於標題,其實這是兩個問題。因為途徑的不同所以做法不同,所以我有不同程度的疑問。

    第一個就是我透過超連結去執行相對的方法(裡面寫好了lin-q),這個方法找出了相關結果用ViewBag記下後,我用RedirectToAction到達要呈現的頁面那個方法,但頁面沒有任何的動靜(就跟我沒按"超連結"去搜尋似的)。

    我專門看過程式是否有進去條件裡,並且結果是有的,所以我不明白為什麼沒有跑我要的結果出來。

    View(部分列出):操作就是我去點了偽裝成下拉選單的超連結,理論上應該要出現if裡的過濾結果

    <div class="row">
                    <div class=" col-md-6">
    
                        <div class="btn-group btn-group-success input-group-lg">
                            <button data-toggle="dropdown" class="btn btn-success dropdown-toggle" type="button">
                                店家分區尋找
                                <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu">
                                @foreach (var area in ViewBag.area)
                                {
                                    <li><a href="/Favorite/searchStore?keyword=@area">@area</a></li>
                                }
                            </ul>
                        </div>
    
                        &nbsp;
    
                        <div class="btn-group btn-group-success input-group-lg">
                            <button data-toggle="dropdown" class="btn btn-success dropdown-toggle" type="button">
                                餐廳分類尋找
                                <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu">
                                @foreach (var cate in ViewBag.cate)
                                {
                                    <li><a href="/Favorite/searchStore?keyword=@cate">@cate</a></li>
                                }
                            </ul>
                        </div>
                    </div>
    
                </div>
            </div>
        </div>
    
    <div class="col-md-12">
    
                        @if (ViewBag.ListResult == 1)
                        {
                            foreach (var result in ViewBag.ListResult)
                            {
                                <div class="col-md-4">
                                    <div class="panel panel-primary">
                                        <div class="panel-heading">
                                            <a href="~/Consumer/storeInfo?orgID=@result.user.orgID&userID=@result.user.userID">
                                                <h3 class="panel-title " style="text-align:center;color:white">
                                                    @result.user.userName
                                                </h3>
                                            </a>
                                        </div>
                                        <div class="panel-body">
                                            <div class="list">
                                                <img src="@Url.Action("userImg", "Favorite", new { oi = result.user.orgID, ui = result.user.userID })"
                                                     onclick="location.href='@Url.Action("storeInfo", "Consumer", new { orgID = result.user.orgID, userID = result.user.userID })'"
                                                     class="img-responsive" />
                                            </div>
                                            <div class="text">
                                                <p>種類:@result.user.category</p>
                                                <p>地址:@result.user.userAddress</p>
                                            </div>
                                            <div>
                                                <h class="pull-left" style="font-size:17px">
                                                    評價分數:
                                                    <span class="star " style="color:yellow;font-size:20px">
                                                        <i class="glyphicon glyphicon-star"></i>
                                                        <!--<i class="glyphicon glyphicon-star-empty"></i>-->
                                                    </span>
                                                </h>
                                            </div>
                                            <div class="pull-right">
                                                <p>
                                                    <i class=" btn btn-danger glyphicon glyphicon-heart" title="加入我的最愛"
                                                       onclick="location.href='@Url.Action("addRestaurantFavotite", "Favorite", new { orgID = result.user.orgID, userID = result.user.userID, page = "restaurant" })'"></i>
                                                    <i class=" btn btn-primary glyphicon glyphicon-pencil" title="我要訂位"
                                                       onclick="location.href='@Url.Action("createreserve", "Consumer", new { orgID = result.user.orgID, userID = result.user.userID })'">我要訂位</i>
                                                </p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            }
                        }
                        else
                        {
                            foreach (var user in Model)
                            {
                                <div class="col-md-4">
                                    <div class="panel panel-primary">
                                        <div class="panel-heading">
                                            <a href="~/Consumer/storeInfo?orgID=@user.orgID&userID=@user.userID">
                                                <h3 class="panel-title " style="text-align:center;color:white">
                                                    @user.userName
                                                </h3>
                                            </a>
                                        </div>
                                        <div class="panel-body">
                                            <div class="list">
                                                <img src="@Url.Action("userImg", "Favorite", new { oi = user.orgID, ui= user.userID })"
                                                     onclick="location.href='@Url.Action("storeInfo", "Consumer", new { orgID = user.orgID, userID = user.userID })'"
                                                     class="img-responsive" />
                                            </div>
                                            <div class="text">
                                                <p>種類:@user.category</p>
                                                <p>地址:@user.userAddress</p>
                                            </div>
                                            <div>
                                                <h class="pull-left" style="font-size:17px">
                                                    評價分數:
                                                    <span class="star " style="color:yellow;font-size:20px">
                                                        <i class="glyphicon glyphicon-star"></i>
                                                        <!--<i class="glyphicon glyphicon-star-empty"></i>-->
                                                    </span>
                                                </h>
                                            </div>
                                            <div class="pull-right">
                                                <p>
                                                    <i class=" btn btn-danger glyphicon glyphicon-heart" title="加入我的最愛"
                                                       onclick="location.href='@Url.Action("addRestaurantFavotite", "Favorite", new { orgID = user.orgID, userID = user.userID, page = "restaurant" })'"></i>
                                                    <i class=" btn btn-primary glyphicon glyphicon-pencil" title="我要訂位"
                                                       onclick="location.href='@Url.Action("createreserve", "Consumer", new { orgID = user.orgID, userID = user.userID})'">我要訂位</i>
                                                </p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            }
                        }
                    </div>

    Controller:

            //店家搜尋 << 下拉選單
            public ActionResult searchStore(String keyword)
            {
                var query = db.coupon.AsQueryable();
                int doSearch = 0;
                if (!string.IsNullOrWhiteSpace(keyword))
                {
                    query = query.Where(x => x.User.userName.Contains(keyword) || x.User.userArea.Contains(keyword));
                    ViewBag.result = query;
                    doSearch = 1;
                }
                return RedirectToAction("restaurant", new { doSearch = doSearch });
            }
    
            //店家列表
            public ActionResult restaurant(int? hint, int? doSearch)
            {
                if(hint.HasValue)
                    ViewBag.alert = hint;
    
                //地區
                var area = (from p in db.User select p.userArea).Distinct();
                ViewBag.area = area;
                //種類
                var cate = (from p in db.User select p.category).Distinct();
                ViewBag.cate = cate;
    
                if (doSearch == 1)
                {
                    ViewBag.ListResult = ViewBag.result;
                }
    
                return View(db.User.ToList());
            }


    第二個就是我想讓使用者可以自己輸入文字,然後送出表單來做搜尋,但這個做法我想了開頭就糾結成一團,所以只能先列個大概想法來請教。

    糾結的原因主要在於送出後還是要回原來的頁面呈現結果,這邊我直覺邏輯就怪怪的。次要原因是送出表單的[HttpPost]的那隻功能需要的參數好像都是給"一整個資料表",可是我這個textbox送出的只是一個string,他可以去比對資料表的不只一個欄位來做搜尋。

    舉例來說,正常的情況下表單送出的程式長這樣:

    (Controller---這是一個新增資料的表單,跟我要的做搜尋的表單本質上不同,但送表單的作法上我是參考這樣的方式)

           [HttpPost]
            public ActionResult create(coupon c, HttpPostedFileBase file)//將圖片存進資料庫
            {
                if (file != null)
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        file.InputStream.CopyTo(ms);
                        byte[] array = ms.GetBuffer();
                        c.couponImage = array;
                    }
                }
                c.User = (from u in db.User where u.orgID == c.orgID && u.userID == c.userID select u).FirstOrDefault();
                db.coupon.Add(c);
                db.SaveChanges();
                
    
                return RedirectToAction("Index");
            }

    這個程式一拿就拿了coupon c 這樣的東西,那換成我自己的話,難道我可以這樣只寫String keyword 嗎?如果可以,內容上我就跟第一個問題大部分的程式相同,可最後要導向原來的頁面,我就還是不知道該怎麼做了。

    該View(相關部分)

                            <form action="/Favorite/restaurant" class="search-form">
                                <div class="form-group has-feedback">
                                    <label for="search" class="sr-only">Search</label>
                                    <input type="text" class="form-control" name="search" id="search" placeholder="快速搜尋餐廳">
                                    <button type="submit">
                                        <span class="glyphicon glyphicon-search form-control-feedback"></span>
                                    </button>
                                </div>
                            </form>

    示意Controller:

            //店家搜尋 << 關鍵字輸入
            [HttpPost]
            public ActionResult restaurant(String keyword)
            {
                //搜尋的方式參照searchStore(),但最後如何導回本頁並呈現搜尋結果待解決
            }

    以上。如果有更好的做法也希望可以告訴我。老實說我覺得這兩種做法內容上很相似卻要寫兩遍,跟重複利用程式的觀念很衝突,可是我卻沒有可以解決的辦法有種彆扭感。

    謝謝。

    2015年10月13日 下午 06:37

解答

所有回覆