【问题标题】:Getting Null Value reference when trying to pass View model尝试传递视图模型时获取空值参考
【发布时间】:2015-07-30 16:10:21
【问题描述】:

尝试将数据传递给视图模型时出现错误空引用异常

视图模型

 public class AccommodationApplicationViewModel
{
    public AccommodationApplicationViewModel() { }

    public PropertyRentingApplication _RentingApplicationModel { get; set; }

    public PropertyRentingPrice _PropertyRentingPriceModel { get; set; }

}

控制器方法

[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");


    }
}

【问题讨论】:

标签: asp.net-mvc-5 asp.net-mvc-viewmodel


【解决方案1】:

您尚未初始化 _RentingApplicationModel_PropertyRentingPriceModel。最简单的解决方案是在 AccommodationApplicationViewModel 的构造函数中简单地初始化这些属性:

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

那么,你应该没事。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2017-06-23
    • 2017-12-10
    相关资源
    最近更新 更多