将List对象直接分配给PagedList时出现Null引用错误

时间:2015-01-22 17:59:01

标签: asp.net-mvc linq nullreferenceexception asp.net-mvc-viewmodel 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;
        }

// ViewModel //

           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;

                }

1 个答案:

答案 0 :(得分:0)

我理解的代码:

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

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