[英]asp.net mvc search with page doesnt work?
我正在嘗試通過搜索和分頁來查看視圖,但是當搜索確定時,但是當我單擊下一頁鏈接時,沒有任何顯示
[HttpGet]
public ActionResult Browse()
{
return View();
}
[HttpPost]
public ActionResult Browse(FormCollection formContent ,int? page)
{
string cartype = !String.IsNullOrEmpty(formContent["Cartype"]) ? formContent "Cartype"] : "";
string SearchBox = !String.IsNullOrEmpty(formContent["searchbox"]) ? formContent["searchbox"] : "";
DateTime toDate = !String.IsNullOrEmpty(formContent["toDate"]) ? DateTime.Parse(formContent["toDate"]) : DateTime.MaxValue;
string Sort = formContent["sort"];
mvc4advertismentEntities2 db = new mvc4advertismentEntities2();
var result = AdvertFunObj.GetAdverts();
switch (Sort)
{
case "":
result = db.Mercedes.Where(m => m.CarType == cartype).ToList();
break;
case "price":
result = db.Mercedes.Where(m => m.CarType == cartype).OrderByDescending(m => m.Price).ToList();
break;
case "date":
result = db.Mercedes.Where(m => m.CarType == cartype).OrderByDescending(m => m.ExpirationDate).ToList();
break;
case "enginecapaity": result = db.Mercedes.Where(m => m.CarType == cartype).OrderByDescending(m => m.EngineCapacity).ToList();
break;
}
int pageSize = 6;
int pageNumber = (page ?? 1);
return View(result.ToPagedList(pageNumber, pageSize));
}
風景
<table class="advertbrowsediv " id="searcht" width="100%"><tr><td style="width: 42%"> فئة السياره :
<br />
<%: Html.DropDownList("Cartype", new SelectListItem [] {
new SelectListItem(){Text="مرسيدس",Value="مرسيدس", Selected=true},
new SelectListItem(){Text="ميتسوبيشي",Value="ميتسوبيشي"},
}) %>
</td>
<td >ترتيب حسب :
<br />
<%: Html.DropDownList("sort", new SelectListItem [] {
new SelectListItem(){Text="",Value="", Selected=true},
new SelectListItem(){Text="التاريخ",Value="date"},
new SelectListItem(){Text="السعر",Value="price"},
new SelectListItem(){Text="سعة المحرك",Value="enginecapaity"},
},
</td><td><br /><input style=" float:right" type="submit" value="بحث>
</table>
該代碼尚不清楚,無論如何,如果您單擊下一頁鏈接,則是執行獲取而不是發布。 因此,您應該在get方法中添加邏輯。
搜索時,您使用的是HttpPost方法,並且在發生搜索和分頁的地方執行方法“ Browse”。
當您點擊下一頁時,您將收到HttpGet請求(我想),而您在“瀏覽(httpPost)中所做的一切都不會發生。
轉到新頁面將需要您保留搜索條件,頁面編號,並且使用該方法,您需要再次獲取數據集並進行分頁。 您可以將它們作為查詢參數傳遞。
您擁有的另一個選項是使您的下一頁請求成為HTTPPost請求,並使其執行“瀏覽”(httpPost)
Fiddler是您的朋友http://www.fiddler2.com/fiddler2/運行它,您將看到確切地發送到服務器的內容,使用的動詞等。
希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.