Asp.net MVC 5.0错误:对象引用未设置为对象的实例。

Asp.net MVC 5.0错误:对象引用未设置为对象的实例。,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,我正在使用MVC音乐商店模式建立一个在线食品订购网站。原来的音乐商店几乎没有什么改进,但我有一个问题,我没有地方去寻找解决方案 我的购物车页面引发以下错误:对象引用未设置为对象的实例。 此错误的原因是:我的食物对象为null(类似于Album类) @foreach(Model.CartItems中的变量项) { @项目.食品.描述(简称.ToUpper)() @ActionLink(item.Food.descr_缩写,“详细信息”,“存储”,新的{id=item.FoodId},null) 公

我正在使用MVC音乐商店模式建立一个在线食品订购网站。原来的音乐商店几乎没有什么改进,但我有一个问题,我没有地方去寻找解决方案

我的购物车页面引发以下错误:对象引用未设置为对象的实例。 此错误的原因是:我的食物对象为null(类似于Album类)

@foreach(Model.CartItems中的变量项)
{
@项目.食品.描述(简称.ToUpper)()
@ActionLink(item.Food.descr_缩写,“详细信息”,“存储”,新的{id=item.FoodId},null)

公共级食品
{
[关键]
public int food_id{get;set;}
[显示名称(“短描述”)]
[长度(50)]
[必需(ErrorMessage=“需要简短描述”)]
公共字符串descr_short{get;set;}
[显示名称(“说明”)]
[长度(5000)]
[必需(ErrorMessage=“需要食品说明”)]
公共字符串descr{get;set;}
[显示名称(“成分”)]
[长度(1000)]
公共字符串成分{get;set;}
[显示名称(“单位大小”)]
公共整数大小_单位{get;set;}
[显示名称(“每餐单位”)]
公共整数单位每餐{get;set;}
[显示名称(“卡路里”)]
公共整数{get;set;}
[显示名称(“可从”)]
公共日期时间可从{get;set;}获得
[显示名称(“已终止?”)]
公共布尔已中断{get;set;}
[显示名称(“终止日期”)]
公共日期时间?不连续的{get;set;}
[显示名称(“价格”)]
[DisplayFormat(ApplyFormatInEditMode=true,DataFormatString=“{0:c}”)]
[UIHint(“货币”)]
公共十进制价格{get;set;}
[显示名称(“素食者?”)]
公共布尔向量{get;set;}
[显示名称(“可用天数”)]
公共字符串daysAvailable{get;set;}
公共虚拟列表OrderDetails{get;set;}
}
公共类购物车
{
[关键]
public int RecordId{get;set;}
公共字符串CartId{get;set;}
公共int{get;set;}
[必需(AllowEmptyStrings=true,ErrorMessage=”“)]
[范围(0,100,ErrorMessage=“数量必须介于0和100之间”)]
[显示名称(“数量”)]
公共整数计数{get;set;}
public System.DateTime DateCreated{get;set;}
公共虚拟食品{get;set;}
}

你能包括你的两个模型吗?然后确保你在操作方法中将食物属性加载到非空值!张贴我的食物和购物车模型你的属性描述为空或为空。我在addtoCart方法中看到了这一点
 @foreach (var item in Model.CartItems)
        {
            <tr id="row-@item.RecordId">
                <td data-th="Product">
                    <div class="row">
                        <div class="col-sm-2 hidden-xs">
                            <img src='@Url.Content("~/Content/Images/Product/Product64x64_" + @item.FoodId.ToString() + ".png")' />
                        </div>
                        <div class="col-sm-10">
                            <h4 class="nomargin">@item.Food.descr_short.ToUpper()</h4>
                            <p>@Html.ActionLink(item.Food.descr_short, "Details", "Store", new { id = item.FoodId }, null)</p>
                        </div>
                    </div>
                </td>
  public class Food
{
    [Key]
    public int food_id { get; set; }

    [DisplayName("Short Desc")]
    [StringLength(50)]
    [Required(ErrorMessage = "A short description is required")]
    public string descr_short { get; set; }

    [DisplayName("Description")]
    [StringLength(5000)]
    [Required(ErrorMessage = "Food Description is required")]
    public string descr { get; set; }

    [DisplayName("Ingredient")]
    [StringLength(1000)]
    public string ingredient { get; set; }

    [DisplayName("Unit Size")]
    public int size_unit { get; set; }

    [DisplayName("Units Per Meal")]
    public int units_per_meal { get; set; }

    [DisplayName("Calories")]
    public int calories { get; set; }

    [DisplayName("Available From")]
    public DateTime available_from { get; set; }

    [DisplayName("Discontinued?")]
    public bool discontinued { get; set; }

    [DisplayName("Discontinued Date")]
    public DateTime? discontinued_dt { get; set; }

    [DisplayName("Price")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
    [UIHint("Currency")]
    public Decimal price { get; set; }

    [DisplayName("Vegetarian?")]
    public bool veg { get; set; }

    [DisplayName("Days Available")]
    public string daysAvailable { get; set; }
    public virtual List<OrderDetail> OrderDetails { get; set; }


}
 public class Cart
{
    [Key]
    public int RecordId { get; set; }
    public string CartId { get; set; }
    public int FoodId { get; set; }
    [Required(AllowEmptyStrings = true, ErrorMessage = " ")]
    [Range(0, 100, ErrorMessage = "Quantity must be between 0 and 100")]
    [DisplayName("Quantity")]
    public int Count { get; set; }
    public System.DateTime DateCreated { get; set; }
    public virtual Food Food { get; set; }
}