直接将 List 对象分配给 PagedList 时出现 Null Reference Error

Null Reference Error when directly assigning a List object to an PagedList

KeyMaster 模型

        public class KeyMasterModel
        {   
            public int KeyId { get; set; }

            public int TypeId { get; set; }

            public string TypeName { get; set;}

        }

PagedKeyModel

        public class PagedKeyModel
        {
            // Collection of our KeyMasterModel , trying to populate this and send it to ViewModel to display a Grid//

            public IPagedList<Security.Models.KeyMasterModel> pagedkeymaster;
        }

//视图模型//

           public class KeyMasterViewModel
        {

//告诉我如何在此处的构造函数中初始化 KeyMasterViewModel.pagedkeymodel.pagedkeymaster,这样我就不会得到 null

            public PagedKeyModel pagedkeymodel;

            public KeyMasterModel keymastermodel;

        }

//控制器//

                 [HttpGet]
                public ActionResult ListOfKey(string sortOrder, string CurrentSort, int? page)
                {    

// View Model Object //

KeyMasterViewModel keyMasterViewModel = new KeyMasterViewModel();


    // At the gollowing step keyMasterModelObject has values retrieved from db //

//Next aim is to place it into ViewModel Object by assigning the retrieved PagedList of KeyMasterModel to IPagedList<Security.Models.KeyMasterModel> pagedkeymaster


  IPagedList<KeyMasterModel> KeyMasterModelObject= datalayercall.GetAll(sortOrder, CurrentSort, page);

    // Here is where error is thrown , all of a sudden I get the error , KeyMasterModelObject becomes null .

    // I am trying finally to populate everything into ViewModel object

          keyMasterViewModel .pagedkeymodel.pagedkeymaster = KeyMasterModelObject;

          return View(keyMasterViewModel);
        }

// 业务逻辑层

         public IPagedList<KeyMasterModel> GetAll(string sortOrder, string CurrentSort, int? page)
                {

                    var retrieveddatalayerobject = datalayerobject.KeyMasters;

    // Retrieving Data from db and forming a list according to my model in App//

                    List<KeyMasterModel> keymastermodellist = new List<KeyMasterModel>();

                    KeyMasterModel keymastermodelobject=new KeyMasterModel();

                    foreach(var retrieveditems in retrieveddatalayerobject)
                    {
                        keymastermodelobject.KeyId = retrieveditems.KeyId;
                        keymastermodelobject.TypeName = retrieveditems.TypeMaster.TypeName;

// Create a New List Of KeyMasterModel //

                       keymastermodellist.Add(keymastermodelobject);
                    }

    // Paged List of KeyMaster Model //

                    IPagedList<KeyMasterModel> IPagedListKeyMasterModel = null; 

                    switch (sortOrder)
                    {

                        case "KeyId":
                            if (sortOrder.Equals(CurrentSort))

                                IPagedListKeyMasterModel = keymastermodellist.OrderByDescending
                                        (m => m.KeyId).ToPagedList(pageIndex, pageSize);

                    }

                    return IPagedListKeyMasterModel;

                }

据我了解你的代码:

KeyMasterViewModel keyMasterViewModel = new KeyMasterViewModel();
[...]
keyMasterViewModel.pagedkeymodel.pagedkeymaster = KeyMasterModelObject;

所以在这里您使用 keyMasterViewModel.pagedkeymodel 并假设它不为空(因为您正试图在其上分配给 属性)。但是 KeyMasterViewModel 没有构造函数,也没有在声明时将 pagedkeymodel 设置为任何内容,因此它将默认为 null。因此你的问题。