Null exception when try to display data from viewmodel in view ASP.NET MVC

Viewed 149

I am trying to display data in my view using a ViewModel however I am getting null exceptions when doing so and I am not sure what the issue is. Basically I have a user, who's data I wish to display in a profile layout in my view, as well as data from different tables all in the same profile view.

ViewModel Code:

public int DonatorID { get; set; }
public string DonatorName { get; set; }
public string DonatorSurname { get; set; }
public string DonatorEmail { get; set; }
public int ContactNo { get; set; }
public int UserID { get; set; }

public string DonatorRank { get; set; }
public string RankIcon { get; set; }
public int XpAmount { get; set; }
public int TokenBalance { get; set; }

public int PaymentID { get; set; }
public string CardNo { get; set; }
public int ExpirationMonth { get; set; }
public int ExpirationYear { get; set; }
public int CVV { get; set; }
public int Zip { get; set; }
public string CardType { get; set; }
public int PaymentDonatorID { get; set; }

public int NpoID { get; set; }
public string NpoName { get; set; }
public double Longitude { get; set; }
public double Latitude { get; set; }

Controller Code:

id = 1;
//Disable lazy loading
db.Configuration.ProxyCreationEnabled = false;

//Add data to the dynamic object
var DynamicProfileDetail = db.tblDonator.Where(zz => zz.user_id == id).Include(yy => yy.tblUser).Include(xx => xx.tblDonator_Rank).FirstOrDefault();

DonatorUserViewModel ProfileDetails = new DonatorUserViewModel();
//Set profile details values
ProfileDetails.DonatorName = DynamicProfileDetail.donator_name;
ProfileDetails.DonatorSurname = DynamicProfileDetail.donator_surname;
ProfileDetails.DonatorEmail = DynamicProfileDetail.tblUser.email;
ProfileDetails.ContactNo = Convert.ToInt32(DynamicProfileDetail.donator_contact_no);

//Set other information
ProfileDetails.XpAmount = DynamicProfileDetail.xp_amount;
ProfileDetails.DonatorRank = DynamicProfileDetail.tblDonator_Rank.description;
ProfileDetails.RankIcon = DynamicProfileDetail.tblDonator_Rank.rank_icon;
ProfileDetails.TokenBalance = DynamicProfileDetail.donator_token_balance;

//Payment details section
int DonatorID = DynamicProfileDetail.donator_id;
var DynamicPaymentDetails = db.tblDonator_Payment_Details.Where(gg => gg.donator_id == DonatorID).Include(ff => ff.tblCard_Type).FirstOrDefault();

DonatorUserViewModel PaymentDetails = new DonatorUserViewModel();

//Set payment detail values
PaymentDetails.CardNo = DynamicPaymentDetails.donator_card_no;
PaymentDetails.ExpirationMonth = DynamicPaymentDetails.expiration_month;
PaymentDetails.ExpirationYear = DynamicPaymentDetails.expiration_year;
PaymentDetails.CVV = DynamicPaymentDetails.cvv;
PaymentDetails.Zip = DynamicPaymentDetails.zip_code;
PaymentDetails.CardType = DynamicPaymentDetails.tblCard_Type.description;

//Map marker section
var DynamicMapMarkers = db.tblNpo;

DonatorUserViewModel MapMarkers = new DonatorUserViewModel();

//Set map marker values
foreach (var item in DynamicMapMarkers)
{
   MapMarkers.NpoID = item.npo_id;
   MapMarkers.NpoName = item.npo_name;
   MapMarkers.Longitude = Convert.ToDouble(item.longitude);
   MapMarkers.Latitude = Convert.ToDouble(item.latitude);

   MapMarkerList.Add(MapMarkers);
}

//ViewBags to display the relevant info
ViewBag.MapMarkerList = MapMarkerList;

return View();

My logic in the code above is that I am setting the values from my database to the objects in the ViewModel and then would be able to display it in my view.

View Code:

@model MyProject.ViewModels.DonatorUserViewModel

<h2>View/Edit your Details:</h2>
<ul>
   <li>First Name: @Model.DonatorName</li>
   <li>Surname: @Model.DonatorSurname</li>
   <li>Contact Number: @Model.ContactNo</li>
   <li>Email Address: @Model.DonatorEmail</li>
</ul>

Sorry for the amount of code in the question (particulary the viewmodel and controller sections) I just need to show that there are a lot of different objects from different database tables.

Now back to my issue, I am getting a null exception in my view for <li>First Name: @Model.DonatorName</li> and I'm assuming for the rest of them as well. I have a feeling there is something wrong with the way I am trying to use my viewmodel, as I am used to doing it using lists and then looping through that list in the view, however I am not sure how to do it now that I am only displaying essentially one line from the database tables instead of all the lines. Anyway any help would be much appreciated.

0 Answers
Related