Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8712761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:10:42+00:00 2026-06-13T05:10:42+00:00

I am using this view model: public class CaseComplaintsViewModel { public int CasesID {

  • 0

I am using this view model:

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

This is the view that uses the view model.

@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>

This is my controller action that returns this partial view to the main view:

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

After the db.SaveChanges when I try to load the ViewModel I get a null exception on the line ComplaintCode = x.ComplaintCode.ComplaintCodeName

This view model is intended to return a view of CaseComplaints and the ComplaintCode models.

Breakpoints in the code show the ComplaintCode is not loaded. I know I am missing something but not sure what.

Thanks

******EDIT********
This is my case model

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

This is my case complaint model

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

this is my complaintcode model

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

        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:10:43+00:00Added an answer on June 13, 2026 at 5:10 am

    the inserted entity is used in your later select, and its navigation properties can’t be loaded. That only works on entities loaded from database. You if you disposed the object context and created a new one it would work. Or you could try this select instead:

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

    There are also some options to refresh entities from the database, not sure that would work, but it’s one more db query you don’t need. This doesn’t do anything.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

View Model: public class Note { [DataType(DataType.MultilineText)] public string Text { get; set; }
This is my model public class AdministrationModel { public string FirstName { get; set;
Given domain model... public class Entity { public int Id { get; set; }
Here is my model: public class Items { public string Foo { get; set;
I have a view model public class DeviceModelEntryViewModel { public int ID { get;
This is My model: public class MyModel { public List<long> NeededIds { get; set;
I have a view model public class PersonsViewmodel { public string FirstName { get;
I have the following model: public class Person { public string Name { get;
This is my combined model: public class AddArticleModel { public TBL_ARTICLES Article { get;
I have this view @model IEnumerable<ViewModelRound2> ... <snip> ... @{ using (Html.BeginForm(new { round1Ring3Bids

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.