【发布时间】:2014-11-13 12:14:14
【问题描述】:
这是我的观点
@using(@Html.BeginForm("CrmBlogGroupType","knowledge",FormMethod.Get)){
@Html.TextBox("search")
@Html.Hidden("type", (string)ViewBag.type)
@Html.DropDownList("PageSize",
new List<SelectListItem>()
{
new SelectListItem ()
{
Text="--Select Page Size--" ,Value="10",Selected=true
},
new SelectListItem ()
{
Text="View 20 records" ,Value="20"
},
new SelectListItem ()
{
Text="View 50 records" ,Value="50"
},
new SelectListItem ()
{
Text="View 100 records" ,Value="100"
},
})
<input type="submit" value="search" id="Searchbtn" />
<br />
@Html.CheckBox("Name")<text>Author Name</text>
@Html.CheckBox("AuthorTitle")<text>Title</text>
@Html.CheckBox("Description")<text>Description</text>
}
这是 PagedList 代码
@Html.PagedListPager(Model, page => Url.Action("CrmBlogGroupType",
new {page,Name=Request.QueryString["Name"].ToLower().Contains("true"),
AuthorTitle=Request.QueryString["AuthorTitle"].ToLower().Contains("true"),
Description=Request.QueryString["Description"].ToLower().Contains("true"), search=Request.QueryString["search"],PageSize=Request.QueryString["PageSize"],type=Request.QueryStrin g["type"]}),new PagedListRenderOptions()
{
DisplayLinkToFirstPage=true,DisplayLinkToLastPage=true,DisplayPageCountAndCurrentLocation=true,Displa yItemSliceAndTotal=true
,DisplayEllipsesWhenNotShowingAllPageNumbers=true,MaximumPageNumbersToDisplay=10
})
控制器代码
public ActionResult CrmBlogGroupType(int? page, bool? Name, bool? AuthorTitle, bool?Description, string search, int? PageSize, string type)
{
if (type==null)
{
//setting the Value in the initial call
//If the SP has changed then make the type parameter as the INT
type = "A";
}
IEnumerable<Usp_getBlogSetPosts_Result> _objBlogSet = _dataLayer.GetBlogSet(type).ToList().ToPagedList(page ?? 1, PageSize ?? 10);
return View(_objBlogSet);
}
出现错误:
对象引用未设置为对象的实例。
Line 202: @if (ViewBag.Search!=null && ViewBag.Search!=string.Empty)
Line 203:{
Line 204:@Html.PagedListPager(Model, page => Url.Action("CrmBlogGroupType", new { page,
Line 205:Name=Request.QueryString["Name"].ToLower().Contains("true"),AuthorTitle=Request.QueryString["Auth orTitle"].ToLower().Contains("true"),
Line 206:Description=Request.QueryString["Description"].ToLower().Contains("true"),
我已经浏览了一些链接,我可以通过这些链接编写这样的代码,最后卡在这里 对此的任何帮助都非常感谢..
【问题讨论】:
-
Request.QueryString["Something"]可以一直是null如果它在请求 url 中不存在,并且当您尝试执行ToLower()到null时会导致异常,您有对它们进行空值检查,或者找出一种方法来避免当您需要的东西为空时视图在控制器中呈现。 -
每次我看到该错误都是由于使用了未实例化的列表。确保您已对所有列表执行此操作。
-
使用 ViewBag 将各种参数传递给
PagedListPager。计算控制器中的值,并且在视图中没有复杂的逻辑。从查询字符串中提取参数,当控制器具有强类型值时,是一种浪费。
标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 razor pagedlist