1

我有一个模型,它在 ASP.NET MVC 3 和 Entity Framework 5.0 中表示有关大学的各种信息。该模型具有另一个模型的 ICollection,称为 TrendModel。无论我做什么,这个集合似乎在任何时候都不会被 MVC 存储/绑定。

当我在运行时(从数据库中检索到它之后)手动将此集合设置为某个值时,该集合当然不再为空,但是无论我将其设置为然后存储在数据库中,趋势始终为空我从数据库中检索它。

大学型号:

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

趋势模型:

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

不确定它是否相关,但如果我为 UniversityModel 放入一个将趋势设置为空列表的构造函数,则趋势不再为空,而是一个空列表。

这是模型绑定问题,还是帖子问题或其他问题?抱歉,如果我完全偏离基础,我对 MVC 和 ASP.NET 还是很陌生。

4

2 回答 2

0

您尚未在趋势模型中包含外键。请尝试univ_id在您的TrendModel课程中添加。

public class TrendModel
{
    [Key]
    public string id { get; set; }
    .
    .
    .
    [ForeignKey("univ_id")]
    public string univ_id   {get;set;}
}
于 2012-12-06T18:07:50.403 回答
0

事实证明,我只是通过对趋势执行延迟加载来解决问题,因此该属性现在显示为:

public virtual ICollection<TrendModel> trends { get; set; }
于 2012-12-06T22:23:55.010 回答