使用ViewModel给出null异常错误

时间:2012-10-19 16:41:07

标签: c# asp.net asp.net-mvc-3 razor entity-framework-4

我正在使用此视图模型:

public class CaseComplaintsViewModel
{
    public int CasesID { get; set; }
    public int CaseComplaintID { get; set; }
    public string ComplaintCode { get; set; }
    public string ComplaintType { get; set; }
}

这是使用视图模型的视图。

@model IEnumerable<cummins_db.ViewModels.CaseComplaintsViewModel>

<table width="100%">
    <tr>
        <th></th>
        <th></th>
        <th>Complaint Code</th>
        <th>Complaint Description</th>
        <th>Delete</th>
    </tr>  
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.HiddenFor(modelItem => item.CasesID )    
            </td>
            <td>
                @Html.HiddenFor(modelItem => item.CaseComplaintID )    
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ComplaintCode )    
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ComplaintType)
            </td>
            <td>
                @Ajax.ActionLink("Delete", "RemoveCodeFromCase", "Cases", new {    caseid           item.CasesID, id = item.CaseComplaintID }, null)
            </td>        
        </tr>
    }
</table>

这是我的控制器操作,将此局部视图返回到主视图:

public ActionResult SelectForCase(int caseid, int compid)
{
    if (ModelState.IsValid)
    {
        CaseComplaint c = new CaseComplaint
        {
            CasesID = caseid,
            ComplaintCodeID = compid
        };
        db.CaseComplaints.Add(c);
        db.SaveChanges();

        var data = (from C in db.CaseComplaints
                    where C.CasesID == caseid
                    select C).ToList().Select( x => new CaseComplaintsViewModel()
                    {
                        CasesID = x.CasesID,
                        CaseComplaintID = x.ComplaintCodeID,
                        ComplaintCode = x.ComplaintCode.ComplaintCodeName,
                        ComplaintType = x.ComplaintCode.ComplaintType
                    }).ToList();

        return PartialView("_CaseComplaintCodes", data);
    }

    return PartialView("_CaseComplaintCodes");
}

在我尝试加载ViewModel的db.SaveChanges之后,我在行ComplaintCode = x.ComplaintCode.ComplaintCodeName上获得了一个空例外

此视图模型旨在返回CaseComplaints和ComplaintCode模型的视图。

代码中的断点显示未加载ComplaintCode。我知道我错过了什么,但不确定是什么。

由于

** * *** 修改 * ** * ** * * 这是我的案例模型

public class Cases
    {
    //case data model for call center
    //implement lists for all related child tables too

    [Key]
    public int CasesID { get; set; }

    public string CaseNumber { get; set; }

    [Required(ErrorMessage = "Customer is Required")]
    public int CustomerID { get; set; }
    public virtual Customer Customer { get; set; }

    [MaxLength(50)]
    public string UserName { get; set; }  //get user name from the aspnet membership

    [Required(ErrorMessage = "Case Category is Required")]
    public int CaseCategoryID { get; set; }

    public int TechnicianID { get; set; }
    public virtual Technician Technicians { get; set; }

    [Required(ErrorMessage = "Engine Model is Required")]
    public int EngineModelID { get; set; }
    public virtual EngineModel EngineModel { get; set; }

    [MaxLength(50)]
    public string BMSWorkorder { get; set; }

    [MaxLength(50)]
    [Required(ErrorMessage = "Status is Required")]
    public string CaseStatus { get; set; }

    [MaxLength(50)]
    public string OpenedBy { get; set; }

    [Required(ErrorMessage = "Opened Date is Required")]
    [DataType(DataType.DateTime)]
    public DateTime? OpenedDate { get; set; }

    [MaxLength(50)]
    public string ClosedBy { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime? ClosedDate { get; set; }

    [MaxLength(50)]
    [Required(ErrorMessage="Caller First Name is Required")]
    public string CallerFirstName { get; set; }

    [MaxLength(50)]
    [Required(ErrorMessage = "Caller Last Name is Required")]
    public string CallerLastName { get; set; }

    [MaxLength(100)]
    public string AdditionalContact { get; set; }

    [MaxLength(10)]
    [Required(ErrorMessage = "Qualified is Required")]
    public string Qualified { get; set; }

    public string Description { get; set; }

    [MaxLength(50)]
    [Required(ErrorMessage = "ESN is Required")]
    public string ESN { get; set; }

    [MaxLength(50)]
    [Required(ErrorMessage = "Mileage is Required")]
    public string Mileage { get; set; }

    [DataType(DataType.Date)]
    public DateTime? DateInService { get; set; }

    [MaxLength(50)]
    public string ESTR { get; set; }

    [MaxLength(50)]
    public string EDS { get; set; }

    [MaxLength(50)]
    public string GensetSerialNumber { get; set; }

    [MaxLength(50)]
    public string GensetModelNumber { get; set; }

    //child Case Notes records
    public virtual ICollection<CaseNotes> CaseNotes { get; set; }

    //child case attachment records
    public virtual ICollection<Attachment> Attachments { get; set; }

    //child case complaint records
    public virtual ICollection<CaseComplaint> CaseComplaint { get; set; }

    //tracking fields
    public DateTime? CreatedOn { get; set; }
    [MaxLength(50)]
    public string CreatedBy { get; set; }
    public DateTime? ModifiedOn { get; set; }
    [MaxLength(50)]
    public string ModifiedBy { get; set; }
    }

这是我的案例投诉模型

public class CaseComplaint
    {
    [Key]
    public int CaseComplaintID { get; set; }
    public int CasesID { get; set; }
    public int ComplaintCodeID { get; set; }
    public virtual Cases Cases { get; set; }
    public virtual ComplaintCode ComplaintCode { get; set; }
    }

这是我的投诉代码模型

public class ComplaintCode
        {
        public int ComplaintCodeID { get; set; }

        [MaxLength(50)]
        [Required(ErrorMessage="Complaint Code is required")]
        public string ComplaintCodeName { get; set; }

        [MaxLength(50)]
        [Required(ErrorMessage="Complaint Type is required")]
        public string ComplaintType { get; set; }

        public virtual ICollection<CaseComplaint> CaseComplaint { get; set; }

        }

2 个答案:

答案 0 :(得分:1)

来自您的数据上下文的数据存在问题。使用Visual Studio断点,检查db.CaseComplaints.ToList()并查看每个项目都有一个非空的有效ComplaintCode属性。

答案 1 :(得分:1)

插入的实体将在您以后的选择中使用,并且无法加载其导航属性。这仅适用于从数据库加载的实体。如果你处理了对象上下文并创建了一个新的对象上下文,它就可以了。或者你可以尝试这个选择:

var data = (from C in db.CaseComplaints
            where C.CasesID == caseid
            select new CaseComplaintsViewModel()
                    {
                        CasesID = C.CasesID,
                        CaseComplaintID = C.ComplaintCodeID,
                        ComplaintCode = C.ComplaintCode.ComplaintCodeName,
                        ComplaintType = C.ComplaintCode.ComplaintType
                    }).ToList();

还有一些选项可以刷新数据库中的实体,不确定它是否可行,但它还有一个你不需要的数据库查询。这没有任何作用。