I have below Model
public class UploadImageAlbum
{
public UploadImageAlbum()
{
Albums = new SelectList(Enumerable.Empty<SelectListItem>());
}
public List<HttpPostedFileBase> Images { get; set; }
public string AlbumID { get; set; }
public SelectList Albums { get; set; }
}
public class Album
{
public string AlbumID { get; set; }
public string AlbumName { get; set; }
}
public class Repository
{
public static List<Album> FetchAlbums()
{
List<Album> albums = new List<Album>
{
new Album(){ AlbumID = "Album1", AlbumName = "Album 1 desc"},
new Album(){ AlbumID = "Album2", AlbumName = "Album 2 desc"}
};
return albums;
}
}
and I call this UploadImageAlbum
in a separate model called AdminViewModel
as below:
public class AdminViewModel
{
public UploadImageAlbum UIAModel { get; set; }
}
and in Controller
I try to fill this model
as below:
public PartialViewResult GetMediaUploadView()
{
AdminViewModel model = new AdminViewModel();
List<Album> albums = Repository.FetchAlbums();
model.UIAModel.Albums = new SelectList(albums, "AlbumID", "AlbumName");//Error here
//Object reference not set to an instance of the object
return PartialView("_UploadMedia", model);
}
I also tried
public PartialViewResult GetMediaUploadView()
{
AdminViewModel model = new AdminViewModel();
List<Album> albums = new List<Album>(); //Creating as new List
albums = Repository.FetchAlbums();//then assign
model.UIAModel.Albums = new SelectList(albums, "AlbumID", "AlbumName");
return PartialView("_UploadMedia", model);
}
But again, I am getting same error. No idea for what reason I am getting this error, even after initializing in model level as well as Controller level. Hope someone might have good knowledge on this. Below is the screen shot of the error I am getting.
Update
Well I don't think this is the duplicate, as I am too aware that there are 'n' number of questions based on null reference
, But the level where we get that null reference
is what makes this question unique. Also I was in impression that, the SelectList
was throwing error, turned out the problem to be different here. So I stand by with my question