0

I getting error null reference exception while trying to pass data to view model

ViewModel

 public class AccommodationApplicationViewModel
{
    public AccommodationApplicationViewModel() { }

    public PropertyRentingApplication _RentingApplicationModel { get; set; }

    public PropertyRentingPrice _PropertyRentingPriceModel { get; set; }

}

Controller Method

[Authorize]
    [HttpGet]
    public ActionResult ApplyForAccommodation()
    {

        int _studentEntityID = 0;

        //PropertyRentingApplication PropertyRentingApplicationModel = new PropertyRentingApplication();

        AccommodationApplicationViewModel PropertyRentingApplicationModel = new AccommodationApplicationViewModel();

        if (User.Identity.IsAuthenticated)
        {
            _studentEntityID = _studentProfileServices.GetStudentIDByIdentityUserID(User.Identity.GetUserId());

            if (_studentEntityID != 0)
            {
                bool StudentCompletedProfile = _studentProfileServices.GetStudentDetailedProfileStatusByID(_studentEntityID);

                if (StudentCompletedProfile)
                {

                    PropertyRentingApplicationModel._RentingApplicationModel.StudentID = _studentEntityID;

                    PropertyRentingApplicationModel._RentingApplicationModel.DateOfApplication = DateTime.Now;

                    var s = "dd";

                    ViewBag.PropertyTypes = new SelectList(_propertyManagementServices.GetAllPropertyType(), "PropertyTypeID", "Title");

                  //  ViewBag.PropertyRentingPrise = _propertyManagementServices.GetAllPropertyRentingPrice();

                    return PartialView("ApplyForAccommodation_partial", PropertyRentingApplicationModel);
                }
                else
                {
                    return Json(new { Response = "Please Complete Your Profile Complete Before Making Request For Accommodation", MessageStatus ="IncompletedProfile"}, JsonRequestBehavior.AllowGet);
                }

            }
            return Json(new { Response = "User Identification Fail!", MessageStatus = "IdentificationFail" }, JsonRequestBehavior.AllowGet);
        }

        return RedirectToAction("StudentVillageHousing", "Home");


    }
}
Toxic
  • 4,369
  • 19
  • 79
  • 183
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Jul 30 '15 at 23:45

1 Answers1

1

You haven't initialized either _RentingApplicationModel nor _PropertyRentingPriceModel. The easiest solution is to simply initialize these properties in your constructor for AccommodationApplicationViewModel:

public AccommodationApplicationViewModel()
{
    _RentingApplicationModel = new PropertyRentingApplication();
    _PropertyRentingPriceModel = new PropertyRentingPrice();
}

Then, you should be fine.

Chris Pratt
  • 207,690
  • 31
  • 326
  • 382