我正在尝试在 MVC Controller 中实现过滤和分页。我使用 ViewBag 将信息传递给 View。
过滤器类是这样的:
public class Criteria
{
public int? SnapshotKey { get; set; }
public string Delq { get; set; }
public override string ToString()
{
string[] Ares = new string[2];
if (SnapshotKey.HasValue)
Ares[0] = "SnapshotKey=" + SnapshotKey.ToString();
if (!String.IsNullOrWhiteSpace(Delq))
Ares[1] = "Delq=" + Delq;
return String.Join("&", Ares.Where(s => !string.IsNullOrWhiteSpace(s)));
}
}
我的 Controller 方法是这样的:
public ActionResult Search(Criteria filter, string filterString,
int pageNo = 1, int pageSize = 5)
{
using (DemoDBEntities db = new DemoDBEntities())
{
var list = db.BMH.AsQueryable();
if (filter != null)
{
if (filter.SnapshotKey.HasValue)
list = list.Where(r => r.SnapshotKey == filter.SnapshotKey.Value);
if (!String.IsNullOrWhiteSpace(filter.Delq))
list = list.Where(r => r.Delq == filter.Delq);
}
list = list.OrderBy(r=>r.SnapshotKey).Skip((pageNo - 1) * pageSize).Take(pageSize);
ViewBag.Criteria = filter.ToString();
ViewBag.CriteriaString = filterString;
ViewBag.PageNo = pageNo;
return View(list.ToList());
}
}
据我所知,我无法将 ViewBag 作为对象传递给 Controller ,这就是我使用 filter.ToString() 来存储当前过滤器的原因。
在 View 中,我有以下链接可以转到特定页面,同时保留当前过滤器。
@Html.ActionLink("Next Page", "Search", new { filter = ViewBag, filterString = ViewBag.CriteriaString, pageNo = ViewBag.PageNo + 1, pageSize = 5 })
因此,当从 View 返回时,我将当前过滤器作为字符串获取。现在在 Controller 中我需要将字符串转换为 Criteria 类。这是可行的,但我正在寻找一种更体面的方法来完成我需要的事情。
最佳答案
Search()
方法中 filterString
的值将是格式为 name=value&name=value...
的字符串,因此您可以首先使用 String.Split()
(在 &
字符上)创建 name=value
项的数组,然后再次拆分(在 =
字符上)获取属性名称和值,但这一切都变得困惑,构建整个查询字符串并将其直接绑定(bind)到您的 Criteria 会更容易
模型。
更改模型以包含所有属性,包括 pageNo
和 pageSize`
public class Criteria
{
public Criteria() // perhaps add some defaults?
{
PageNo = 1;
PageSize = 5;
}
public int? SnapshotKey { get; set; }
public string Delq { get; set; }
public int PageNo { get; set; }
public int PageSize { get; set; }
.... // see method below
}
然后,为了使其灵活并允许您添加更多“条件”属性,请使用反射来构建查询字符串
public string ToQueryString(int pageIncrement)
{
List<string> propValues = new List<string>();
foreach(var prop in GetType().GetProperties())
{
var name = prop.Name;
var value = prop.GetValue(this);
if (name == "PageNo")
{
value == (int)value + pageIncrement;
}
if (value != null)
{
propValues .Add(String.Format("{0}={1}", name, value));
}
}
return "?" + String.Join("&", propValues);
}
Controller 中的代码将是
public ActionResult Search(Criteria filter)
{
using (DemoDBEntities db = new DemoDBEntities())
{
var list = db.BMH.AsQueryable();
if (filter != null)
{
if (filter.SnapshotKey.HasValue)
list = list.Where(r => r.SnapshotKey == filter.SnapshotKey.Value);
if (!String.IsNullOrWhiteSpace(filter.Delq))
list = list.Where(r => r.Delq == filter.Delq);
}
list = list.OrderBy(r => r.SnapshotKey).Skip((filter.PageNo - 1) * pageSize).Take(filter.PageSize);
ViewBag.Criteria = filter;
return View(list.ToList());
}
}
然后在 View 中
<a href="@Url.Action("Search")@ViewBag.Criteria.ToQueryString(-1)">Previous</a>
<a href="@Url.Action("Search")@ViewBag.Criteria.ToQueryString(1)">Next</a>
另请注意,您可以只使用
@Html.ActionLink("Next", "Search", (yourAssembly.Criteria)ViewBag.Criteria)
假设 Criteria
仅包含简单属性,这意味着不需要 ToQueryString()
方法。但是,例如,在使用 ActionLink()
之前,您需要递增/递减 PageNo
属性的值
@{ var criteria = (yourAssembly.Criteria)ViewBag.Criteria; }
@{ criteria.PageNo = @criteria.PageNo - 1; }
@Html.ActionLink("Previous", "Search", criteria)
@{ criteria.PageNo = @criteria.PageNo + 2; }
@Html.ActionLink("Next", "Search", criteria)
关于c# - 将过滤器作为对象从 View 传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40853170/