我有一個列表頁面(索引)顯示網格中的項目列表。該頁面由頂部的小表單和底部的網格(列表)組成。該表格可用作過濾列表中顯示的項目的方式。如何檢查作爲表單提交結果或其他方式顯示頁面
我需要檢查提交表單(單擊2個按鈕中的任何一個)或通過單擊來自其他頁面的鏈接或直接在瀏覽器的地址欄中輸入URL來顯示頁面。
的觀點:
@model MyNameSpace.ViewModels.FooFilterViewModel
@{
ViewBag.Title = "Foo Listing";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
@Html.ValidationSummary(true)
<!-- Field controls used for filtering go here -->
<input id="ClearFilter" type="button" value="Clear Filter" />
<input id="ApplyFilter" type="submit" value="Apply Filter" />
}
<!-- Grid displaying the list of foos goes here -->
控制器動作:
public ActionResult Index(FooFilterViewModel fooFilterViewModel)
{
// Retrieve all records
IQueryable<Foo> foos = _DBContext.Foos;
if (ModelState.IsValid)
{
if (/* check if coming from form submission */){
// Do something
}
else
{
// Do something else
}
// Code to filter records go here
foos = foss.Where(f => f...........)
}
fooFilterViewModel.Foos = foos;
return View(fooFilterViewModel);
}
通常情況下,會有一個額外的控制器操作,其中包含'public ActionResult Index()'的方法簽名,這是當您從地址欄中的點擊鏈接或URL條目路由時會被點擊的方法。 –
您只需要在您的ViewModel中添加一個布爾屬性設置爲false,您需要在表單中爲此屬性設置爲true來添加隱藏字段。在控制器中你檢查這個屬性。如果你不管理這個,後來我會發布你的例子 –
謝謝@LucianBumb。如果找不到更好或更優雅的解決方案,我也在考慮使用隱藏的屬性。 – corix010