【发布时间】:2014-10-10 08:58:03
【问题描述】:
[Authorize(Roles="Admin")]
public class AdminController : Controller
{
Propertydb _db = new Propertydb();
public ActionResult Index(int page = 1)
{
var Sale = (from m in _db.Properties
join p in _db.PropertyType on m.Typeofproperty equals p.PropTypeId
join r in _db.BedRoom on m.Roomdetails equals r.BedRoomId
join c in _db.Cities on m.CityId equals c.CityId
join l in _db.Areas on m.Locality_Id equals l.Locality_Id
where m.SaleorRent == "Sale" && m.Rejected == false
select new { m.Name, m.Id,m.Photo,m.Age, m.SaleorRent,m.Status, l.Locality,p.PropertyTypes,m.BuiltUpArea,m.CreatedDate, c.City, m.Propertyvalue, r.BedRoom })
.Select(m => new SrchDetail
{
Id=m.Id,
Name = m.Name,
SaleorRent = m.SaleorRent,
Photo = m.Photo,
Age = m.Age,
City = m.City,
Locality = m.Locality,
Typeofproperty = m.PropertyTypes,
BuiltUpArea = m.BuiltUpArea,
Roomdetails = m.BedRoom,
CreatedDate = m.CreatedDate,
Propertyvalue = m.Propertyvalue,
Status = m.Status
}).ToList();
ViewBag.sale = Sale;
var Rent = (from m in _db.Properties
join p in _db.PropertyType on m.Typeofproperty equals p.PropTypeId
join r in _db.BedRoom on m.Roomdetails equals r.BedRoomId
join c in _db.Cities on m.CityId equals c.CityId
join l in _db.Areas on m.Locality_Id equals l.Locality_Id
where m.SaleorRent == "Rent" && m.Rejected == false
select new { m.Name, m.Id, m.Photo, m.Age, m.Status, m.SaleorRent, l.Locality, p.PropertyTypes, m.BuiltUpArea, m.CreatedDate, c.City, m.Monthlyrent, r.BedRoom })
.Select(m => new SrchDetail
{
Id = m.Id,
Name = m.Name,
SaleorRent = m.SaleorRent,
Photo = m.Photo,
Age = m.Age,
City = m.City,
Locality = m.Locality,
Typeofproperty = m.PropertyTypes,
BuiltUpArea = m.BuiltUpArea,
Roomdetails = m.BedRoom,
CreatedDate = m.CreatedDate,
Monthlyrent = m.Monthlyrent,
Status = m.Status
}).ToList();
ViewBag.rent = Rent;
//IPagedList<property> property_sale = _db.Properties.Where(rents => rents.SaleorRent == "Sale" && rents.Rejected == false).ToList().ToPagedList(page,10);
//IPagedList<property> property_rent = _db.Properties.Where(rents => rents.SaleorRent == "Rent" && rents.Rejected == false).ToList();
ViewData["Sale"] = Sale.ToPagedList(page, 10);
ViewData["Rent"] = Rent.ToPagedList(page, 10);
return View();
}
The cshtml page for the controller is as follows : I have taken PagedLlist in starting of the page. Is it necessary to use IpagedList here . I don't know what is the reason its showing error .
对象引用未设置为对象的实例。
说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
@model PagedList<Property.Models.SrchDetail>
@{
Layout = "~/Views/Shared/_Adminproperty.cshtml";
<link href="@Url.Content("~/Content/PagedList.css")" rel="stylesheet" type="text/css" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script>
$(function () {
$("#tabs").tabs();
});
</script>
}
<div class="accordian-content-container1">
<h2>New Properties</h2>
<div id="tabs">
<div class="tab-container">
<div id="TabbedPanels1" class="TabbedPanels">
<ul class="TabbedPanelsTabGroup">
<li class="TabbedPanelsTab"><a href="#tabs-1">Sale</a></li>
<li class="TabbedPanelsTab"><a href="#tabs-2">Rent</a></li>
</ul>
<div class="TabbedPanelsContentGroup">
<div class="TabbedPanelsContent" id="tabs-1">
<table>
<tr>
<th>Photo</th>
<th>City</th>
<th>Locality</th>
<th>Type of Property</th>
<th>Room Details</th>
<th>Property Value</th>
<th>Built Up Area </th>
<th>Posted On</th>
<th>Status</th>
<th>Action</th>
</tr>
@foreach (var item in ViewData["Sale"] as IPagedList<Property.Models.SrchDetail>)
{
<tr>
<td>
<img src="~/Images/Ads/@item.Photo" alt="" style="height:40px; width:40px;">
</td>
<td>
@item.City
</td>
<td>
@item.Locality
</td>
<td>
@item.Typeofproperty
</td>
<td>
@item.Roomdetails
</td>
<td>
@item.Propertyvalue
</td>
<td>
@item.BuiltUpArea
</td>
<td>
@item.CreatedDate.ToShortDateString()
</td>
<td>
@Html.ActionLink(Html.Encode(item.Status), "Status", new { id = item.Id })
</td>
<td>
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Reject", "Reject", new { id = item.Id })
</td>
</tr>
}
</table>
</div>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, sale = ViewBag.sale }), new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true })
<div class="TabbedPanelsContent" id="tabs-2">
<table width="100%" cellspacing="2" cellpadding="2" border="1" bordercolor="#dfdddd">
<tr>
<th>Photo</th>
<th>City</th>
<th>Locality</th>
<th>Type of Property</th>
<th>Room Details</th>
<th>Monthly Rent</th>
<th>Posted On</th>
</tr>
@foreach (var item in ViewData["Rent"] as IPagedList<Property.Models.SrchDetail>)
{
<tr>
<td>
<img src="~/Images/Ads/@item.Photo" alt="" style="height:40px; width:40px;">
</td>
<td>
@item.City
</td>
<td>
@item.Locality
</td>
<td>
@item.Typeofproperty
</td>
<td>
@item.Roomdetails
</td>
<td>
@item.Monthlyrent
</td>
<td>
@item.CreatedDate.ToShortDateString()
</td>
<td>
@Html.ActionLink(Html.Encode(item.Status), "Status", new { id = item.Id })
</td>
<td>
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Reject", "Reject", new { id = item.Id })
</td>
</tr>
}
</table>
</div>
</div>
</div>
</div>
</div>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, rent = ViewBag.rent }), new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true })
每当我评论 PagedList 助手时,一切正常,但没有显示分页。否则会显示“对象引用未设置为对象的实例”的错误。
【问题讨论】:
-
您没有将任何模型传递给控制器中的视图...
-
否,数据正在通过 ViewData 传递并在 cshtml 中作为 PagedList 类型接收。
-
但是视图中的模型为空,因为您没有将模型传递给控制器中的视图。在控制器中,您将数据放入 ViewData,而不是模型。将模型传递给您的视图以使您的模型不为空,或者在 PagedListPager 中使用 ViewData 而不是模型。
-
你能通过编辑 pagedList 帮助器告诉我吗?
-
使用
return View(Sale.ToPagedList(page, 10););或@Html.PagedListPager(ViewData["Sale"],或Rent
标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4