简体   繁体   English

当我尝试通过 controller 将 model 传递给视图时,我在 ASP.NET 框架中收到 System.ArgumentException

[英]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. System.ArgumentException: '包含路径表达式必须引用在类型上定义的导航属性。 Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.对参考导航属性使用虚线路径,对集合导航属性使用 Select 运算符。 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.基本上,我有一个客户 model 我想通过它,它有 3 个外键(人类、成员、联系人),并且联系人有一个 ICollection 位置,它是联系人 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?我是否必须制作一个 viewModel 来实现它,我错过了什么? 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.我应该澄清一下,当我运行程序并尝试 go 到视图时,我得到了异常。 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() .您不能在属性的属性上使用.Include ,您必须使用.ThenInclude() You cannot include a Where in the .Include either.您也不能在.Include中包含Where I would handle that in the projection of customer using .Select我会在使用.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.当提供给方法的 arguments 之一无效时,将引发此异常。 Which means it has a null value.这意味着它具有 null 值。 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.由于您不允许将 null 值传递给视图,因此您必须确保 model 中没有 null 值。

If you want to allow for null columns to be passed within the model you should add ?如果您想允许 null 列在 model 中传递,您应该添加? 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.这将应用于主要请求的 model 中包含( .include() )的每个 model。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试使用 Regex.Replace 它抛出 System.ArgumentException - When i try to use Regex.Replace it throws out System.ArgumentException 当我传递4个元素的int数组时,小数构造函数将引发System.ArgumentException - Decimal constructor throws System.ArgumentException when I pass int array of 4 elements 为什么在List上调用Sort(IComparer)时会出现System.ArgumentException? - Why do I get a System.ArgumentException when invoking Sort(IComparer) on a List? asp.net mvc如何将视图模型和输入文本传递给控制器​​? - asp.net mvc how can I pass the view model and input text to controller? 异常详细信息:System.ArgumentException:路径中的非法字符。 ASP.NET MVC - Exception Details: System.ArgumentException: Illegal characters in path. ASP.NET MVC OData 使用 $select 和 asp.net 核心抛出 System.ArgumentException - OData using $select with asp.net core throws System.ArgumentException 锁定时发生System.ArgumentException - System.ArgumentException when locking MVC ASP.NET提交给Controller时在View中获取ViewData.Model - MVC ASP.NET Getting back my ViewData.Model in my View when I submit to a Controller 当尝试将复选框添加到 asp.net forms 上的网格视图时出现错误? - when try to add check box to grid view on asp.net forms i get error? C#asp.NET显示查询输出到屏幕。 错误:Oracle.DataAccess.dll中的“System.ArgumentException” - C# asp.NET displaying output of a query to screen. Error: 'System.ArgumentException' in Oracle.DataAccess.dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM