[英]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.