PagedListPager html帮助器在Mvc中不起作用

时间:2014-10-10 08:58:03

标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

    [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帮助程序时,一切正常但分页未显示。否则,它会显示“对象引用未设置为对象实例”的错误。

1 个答案:

答案 0 :(得分: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 })