简体   繁体   中英

I get a System.ArgumentException in ASP.NET Framework when I try to pass a model to the view through a controller

This is the exception I get:

System.ArgumentException: 'The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path'

basically, I have a customer model I want to pass through that has 3 foreign keys (Human, Member, Contact), and the Contact has an ICollection of locations which is a foreign key to the Contact Model. I'm trying to send everything to the view (including the locations) but I get this error. Do I have to make a viewModel to achieve it, what am I missing? These are the codes:

public class Customer 
{
    public int ID { get; set; }
    public int HumanID { get; set; }
    public int MemberID { get; set; }
    public int ContactID { get; set; }
    public Human Human { get; set; }
    public Member Member { get; set; }
    public Contact Contact { get; set; }
}

public class Contact
{
    public int ID { get; set; }
    
    [Required(ErrorMessage = "E-mail is Required")]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "E-mail")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Phone Number is Required")]
    [Display(Name = "Phone Number")]
    public List<string> PhoneNumber { get; set; }

    public int LocationID { get; set; }
    //One user might have many register locations
    public virtual ICollection<Location> Locations { get; set; }     
}

public ActionResult Index()
{
    var customers = db.Customers.Include(c => c.Contact)
        .Include(c => c.Human).Include(c => c.Member)
        .Include(c => c.Contact.Locations
        .Where(l => l.ID == c.Contact.LocationID));               

    return View(customers.ToList());
}

I should clarify that I get the exception when I run the program and try to go to the view. Thank you all in advance!

given you have created your other entities and verify foreign keys exist. You cannot use .Include on a property of a property, you must use .ThenInclude() . You cannot include a Where in the .Include either. I would handle that in the projection of customer using .Select

var customers = db.Customers
     .Include(c => c.Contact)
    .ThenInclude(c => c.Location)
    .Include(c => c.Member)
    .Include(c => c.Human);

This exception is thrown when one of the arguments provided to a method is not valid. Which means it has a null value. Since you're not allowing null values to be passed to the view you should have to make sure that there are no null values in your model.

If you want to allow for null columns to be passed within the model you should add ? before the data type of each column like the following:-

public class Customer 
{
    public int ID { get; set; }
    public int? HumanID { get; set; }
    public int? MemberID { get; set; }
    public int? ContactID { get; set; }
    public Human Human { get; set; }
    public Member Member { get; set; }
    public Contact Contact { get; set; }

}

This will be applied for every model that is included( .include() ) within the main requested model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM