无法在Json-MVC控制器中传递空值
如果没有值,则系统不会传递“null”值无法在Json-MVC控制器中传递空值,json,asp.net-mvc-4,controller,null,Json,Asp.net Mvc 4,Controller,Null,如果没有值,则系统不会传递“null”值 model.SpecialtyTypeDesc = spec.SpecialtyDescription; 如果没有员工的专业值,我无法打开编辑页面。如果员工具有特定价值,则其工作罚款。如果员工的人员数据中没有任何专业知识,我希望将该值显示为Null public JsonResult GetSpecialityDepartment(int id){ EmployeeDepartments getDep = (from c in db.Emplo
model.SpecialtyTypeDesc = spec.SpecialtyDescription;
如果没有员工的专业值,我无法打开编辑页面。如果员工具有特定价值,则其工作罚款。如果员工的人员数据中没有任何专业知识,我希望将该值显示为Null
public JsonResult GetSpecialityDepartment(int id){
EmployeeDepartments getDep = (from c in db.EmployeeDepartments where c.EmpId == id select c).FirstOrDefault();
Departments dept = (from c in db.Departments where c.Id == getDep.departmentId select c).FirstOrDefault();
EmployeeDetails details = (from c in db.EmployeeDetails where c.People_Id == id select c).FirstOrDefault();
SpecialtyType spec = (from c in db.SpecialityType where c.SpecialtyTypeId == details.SpecialtyTypeId select c).FirstOrDefault();
var data = new {
Department = dept.Name,
SpecialtyType = spec == null ? "" : spec.SpecialtyDescription
};
return Json(data);
}
public ActionResult Edit(int id) {
PersonnelLeaveAbsence leaveAbsence = (from c in db.PersonnelLeaveAbsence where c.PersonnelLeaveAbsenceId == id select c).FirstOrDefault();
EmployeeDepartments getDep = (from c in db.EmployeeDepartments where c.EmpId == leaveAbsence.PersonnelId select c).FirstOrDefault();
Departments dept = (from c in db.Departments where c.Id == getDep.departmentId select c).FirstOrDefault();
EmployeeDetails details = (from c in db.EmployeeDetails where c.People_Id == leaveAbsence.PersonnelId select c).FirstOrDefault();
SpecialtyType spec = (from c in db.SpecialtyType where c.SpecialtyTypeId == details.SpecialtyTypeId select c).FirstOrDefault();
PersonnelLeaveAbsenceModel model = new PersonnelLeaveAbsenceModel();
model.PersonnelLeaveAbsenceId = leaveAbsence.PersonnelLeaveAbsenceId;
model.PersonnelLeaveDate = leaveAbsence.LeaveDate;
model.LeaveAbsenceId = leaveAbsence.LeaveAbsenceTypeId;
model.PersonnelId = leaveAbsence.PersonnelId;
model.EmployeeRoleId = leaveAbsence.EmployeeRole;
model.StartTime = leaveAbsence.StartTime;
model.EndTime = leaveAbsence.EndTime;
model.DayType = Convert.ToBoolean(leaveAbsence.DayType);
model.DepartmentDesc = dept.Name;
model.SpecialtyTypeDesc = spec.SpecialtyDescription;
}
不传递“null”值意味着什么?如果为空,则为空!如果您的意思是要在文本为Null时显示Null,则使用属性上的[DisplayFormatNullDisplayText=Null]属性,或者将该值设置为Null。如果原始人员表中有值,则传递该值。如果原始人员表中没有专业值,代码将停止在“model.SpecialtyTypeDesc=spec.SpecialtyDescription;”。它应该显示空字段。谢谢你。你的意思是因为规格为空而得到一个异常吗?是的。异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。在问题中包含这一点很重要,你不这么认为吗。如果spec!=null{model.specialtypedesc=spec.SpecialtyDescription;}