嵌套在模型中的集合不受MVC 3约束(始终为空) [英] Collection Nested within Model is Not Bound by MVC 3 (Always null)
问题描述
当我在运行时手动将该集合设置为某个东西(在从数据库中检索到该集合之后),该集合当然不再是空的,但是无论我看起来如何设置它然后存储在数据库中,当我从数据库中检索时,趋势总是为空。
UniversityModel:
public class UniversityModel
{
[Key]
public string univ_id {get;组; }
public string ipeds_id {get;组; }
public string name {get;组; }
public bool relig {get;组; }
#region位置信息
public string city {get;组; }
public string state {get;组; }
public string urbanization {get;组; }
public double latitude {get;组; }
public double longitude {get;组; }
#endregion
public ICollection< TrendModel>趋势{get;组; }
}
TrendModel:
public class TrendModel
{
[Key]
public string id {get;组; }
public ushort year {get;组; }
public uint?能力{get;组; }
public uint?率{get;组; }
public uint?吃饭{get;组; }
public bool? forProfit {get;组; }
public bool?控制{get;组; }
public string degree {get;组; }
public bool?土地出让人组; }
public bool?运动{get;组; }
public string calendar {get;组; }
public bool?必须{get;组; }
}
不知道是否相关,但是如果我把一个构造函数UniversityModel将趋势设置为空列表,然后趋势不再为空,并且是空列表。
这是一个模型绑定问题,还是后期问题?对不起,如果我完全离基,我是MVC和ASP.NET的新手。
这个问题是由我强制执行对趋势的懒惰加载来解决的,所以该属性现在显示为:
public virtual ICollection< TrendModel>趋势{get;组; }
I have a model that represents various information about a university in ASP.NET MVC 3 and Entity Framework 5.0. The model has an ICollection of another model, called TrendModel. This collection seems to never be stored/bound by MVC at any point, no matter what I do.
When I manually set this collection to something at run time (after it is retrieved from the database), the collection is of course no longer null, but whatever I seem to set it to and then store in the database, trends is always null when I retrieve it from the database.
UniversityModel:
public class UniversityModel
{
[Key]
public string univ_id { get; set; }
public string ipeds_id { get; set; }
public string name { get; set; }
public bool religious { get; set; }
#region Location Information
public string city { get; set; }
public string state { get; set; }
public string urbanization { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
#endregion
public ICollection<TrendModel> trends { get; set; }
}
TrendModel:
public class TrendModel
{
[Key]
public string id { get; set; }
public ushort year { get; set; }
public uint? capacity { get; set; }
public uint? rate { get; set; }
public uint? meals { get; set; }
public bool? forProfit { get; set; }
public bool? control { get; set; }
public string degree { get; set; }
public bool? landgrant { get; set; }
public bool? athletic { get; set; }
public string calendar { get; set; }
public bool? required { get; set; }
}
Not sure if it is relevant, but if I put in a constructor for UniversityModel that sets trends to an empty list, then trends is no longer null and is an empty list.
Is this a model binding issue, or a post issue or something? Sorry if I'm completely off-base, I'm pretty new to MVC and ASP.NET.
As it turn out, the issue was fixed simply by me enforcing lazy loading on the trends, so the property now reads:
public virtual ICollection<TrendModel> trends { get; set; }
这篇关于嵌套在模型中的集合不受MVC 3约束(始终为空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!