Pregunta

I'm trying to sort out this issue but as I'm learning a lot of this stuff as I go along I'd really appreciate it if someone could explain where I'm going wrong and/or some good resources where I can read up.

So, I have a model based on my Entity Framework model of my database and a viewmodel representing properties in that model. I've built a Kendo grid to display the data (defined in a js file) and the method in the contoller returns a Json result set. Trouble is, when I try to display a value in a joined db table, if there hasn't been a key value set, I get a nullreferenceexception error. Obviously I'm missing part of the puzzle here as there must be a way of coding this to stop it happening. Any help would be gratefully received!

My model is like this:

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; }
    }
}

My controller method looks like this:

    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);
    }

and my view model lilke this:

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

    }
}
¿Fue útil?

Solución

Do a null check on the object before setting it, i.e.

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

and you'll have to do similar for status and venue

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;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top