gpt4 book ai didi

c# - MVC View 模型抛出的 NullReferenceException

转载 作者:行者123 更新时间:2023-11-30 17:49:17 26 4
gpt4 key购买 nike

我正在努力解决这个问题,但是随着我的学习,我学到了很多这些东西,如果有人能解释我哪里出错了和/或一些好的资源,我将非常感激我可以阅读。

因此,我有一个基于我的数据库 Entity Framework 模型的模型和一个表示该模型中属性的 View 模型。我已经构建了一个 Kendo 网格来显示数据(在 js 文件中定义)并且 Controller 中的方法返回一个 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; }
}
}

我的 Controller 方法如下所示:

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

我的 View 模型是这样的:

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

}
}

最佳答案

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

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;

关于c# - MVC View 模型抛出的 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21937175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com