2014-10-10 46 views
0
[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 . 

未將對象引用設置爲對象的實例。PagedListPager html幫手不在Mvc中工作

描述:執行當前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助手於一切工作正常,但沒有顯示分頁。否則,它會顯示錯誤:「對象引用未設置爲對象的實例」。

+0

你不能傳遞任何模型中的視圖控制器... – artm 2014-10-10 09:01:17

+0

不,數據通過ViewData的傳遞和CSHTML收到PagedList類型。 – 2014-10-10 09:03:13

+0

但是您視圖中的模型爲空,因爲您沒有將模型傳遞給控制器​​中的視圖。在控制器中,您將數據放入ViewData中,而不是模型。將模型傳遞給您的視圖,以便您的模型不爲null,或者在您的PagedListPager中使用ViewData而不是Model。 – artm 2014-10-10 09:05:44

回答

0
ViewBag.sale = Sale.ToPagedList(page, 10); 
ViewBag.rent = Rent.ToPagedList(page, 10); 

在CSHTML

 @Html.PagedListPager((IPagedList)ViewBag.rent, page => Url.Action("Index", new { page }), new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true }) 

    @Html.PagedListPager((IPagedList)ViewBag.sale, page => Url.Action("Index", new { page }), new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true })