KeyMaster 모델Null 참조 오류 직접 PagedList에 목록 개체를 할당 할 때
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;
}
서식을 정리하고 관련없는 코드를 제거하십시오. 1 또는 2 개의 필드 만 샘플로 표시 할 수 있습니다. – eckes