3

我正在尝试解决这个问题,但是随着我学习很多这些东西,如果有人能解释我哪里出错和/或一些我可以阅读的好资源,我将不胜感激向上。

因此,我有一个基于我的数据库的实体框架模型的模型和一个表示该模型中属性的视图模型。我已经构建了一个 Kendo 网格来显示数据(在 js 文件中定义),并且控制器中的方法返回一个 Json 结果集。麻烦的是,当我尝试在连接的数据库表中显示一个值时,如果没有设置键值,我会收到一个 nullreferenceexception 错误。显然,我在这里遗漏了部分难题,因为必须有一种编码方式来阻止它的发生。任何帮助将不胜感激!

我的模型是这样的:

namespace TrainingKendoUI.Models
{
    using System;
    using System.Collections.Generic;

    public partial class TRAINING_EMPLOYEE_COURSES
    {
        public int EMP_COURSE_ID { get; set; }
        public int EMPLOYEE_ID { get; set; }
        public int COURSE_ID { get; set; }
        public Nullable<System.DateTime> DATE_ATTENDED { get; set; }
        public Nullable<decimal> COURSE_COST { get; set; }
        public string COURSE_RESITS { get; set; }
        public Nullable<int> PROVIDER_ID { get; set; }
        public Nullable<int> EMP_COURSE_STATUS_ID { get; set; }
        public Nullable<int> VENUE_ID { get; set; }

        public virtual TRAINING_COURSES TRAINING_COURSES { get; set; }
        public virtual TRAINING_EMPLOYEE_COURSE_STATUS TRAINING_EMPLOYEE_COURSE_STATUS  { get; set; }
        public virtual TRAINING_EMPLOYEES TRAINING_EMPLOYEES { get; set; }
        public virtual TRAINING_PROVIDERS TRAINING_PROVIDERS { get; set; }
        public virtual TRAINING_VENUES TRAINING_VENUES { get; set; }
    }
}

我的控制器方法如下所示:

    public JsonResult EmployeeCourses_Read()
    {
        var model = db.TRAINING_EMPLOYEE_COURSES;
        var ViewModel = new List<EmployeeCoursesIntersectionViewModel>();

        foreach (var employee in model)
        {
            ViewModel.Add(new EmployeeCoursesIntersectionViewModel(employee));
        }

        return Json(ViewModel, JsonRequestBehavior.AllowGet);
    }

我的视图模型是这样的:

namespace TrainingKendoUI.ViewModels
{
    public class EmployeeCoursesIntersectionViewModel
    {

        #region Constructors

            public EmployeeCoursesIntersectionViewModel()
            {

            }

            public EmployeeCoursesIntersectionViewModel(TRAINING_EMPLOYEE_COURSES  model)
            {
                this.empCourseId = model.EMP_COURSE_ID;
                this.employee = model.TRAINING_EMPLOYEES.FIRST_NAME;
                this.course = model.TRAINING_COURSES.COURSE_NAME;
                this.dateAttended = model.DATE_ATTENDED;
                this.cost = model.COURSE_COST;
                this.resits = model.COURSE_RESITS;
                //These lines will produce a NullReference error if not set through the front end...
                this.provider = model.TRAINING_PROVIDERS.PROVIDER_NAME;
                this.status =    model.TRAINING_EMPLOYEE_COURSE_STATUS.EMP_COURSE_STATUS;
                this.venue = model.TRAINING_VENUES.VENUE_NAME;

        }

        #endregion

        #region Properties

        public int empCourseId { get; set; }
        public string employee { get; set; }
        public string course { get; set; }
        public Nullable<System.DateTime> dateAttended { get; set; }
        public Nullable<decimal> cost { get; set; }
        public string resits { get; set; }
        public string provider { get; set; }
        public string status { get; set; }
        public string venue { get; set; }

        #endregion

    }
}
4

1 回答 1

1

在设置对象之前对其进行空检查,即

this.provider = model.TRAINING_PROVIDERS == null ? "" 
                        : model.TRAINING_PROVIDERS.PROVIDER_NAME;

你必须为状态和地点做类似的事情

this.status =    model.TRAINING_EMPLOYEE_COURSE_STATUS== null ? ""
                        model.TRAINING_EMPLOYEE_COURSE_STATUS.EMP_COURSE_STATUS;
this.venue = model.TRAINING_VENUES== null ? ""
                        model.TRAINING_VENUES.VENUE_NAME;
于 2014-02-21T14:31:42.403 回答