无法在Json - MVC控制器中传递空值

时间:2014-11-21 07:44:23

标签: json asp.net-mvc-4 controller null

系统没有通过' null'如果

没有值,则为值
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;
}

0 个答案:

没有答案