我正在尝试使用mvc 5和EF6从数据库加载下拉列表。当我执行应用程序
时,我不断收到错误“对象引用未设置为对象的实例”错误这是我的模特:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using dtPerson_v10_p1;
using Nevron.Chart;
namespace ds_iDMS.Models.Deals
{
public class WorksheetAfterMarketViewModel
{
[Display(Name = @"Integrated Product:")]
public string integratedProduct { get; set; }
public List<SelectListItem> IntProducts { get; set; }
public List<SelectListItem> CompanyNames { get; set; }
public List<ProductCategoryItem> ProductCategories { get; set; }
public WorksheetAfterMarketViewModel()
{
IntProducts = new List<SelectListItem>();
CompanyNames = new List<SelectListItem>();
}
}
public class ProductCategoryItem
{
[Display(Name = @"Price")]
[DisplayFormat(DataFormatString = "{0:C0}")]
[DataType(DataType.Currency)]
public float Price { get; set; }
[Display(Name = @"Cost")]
[DisplayFormat(DataFormatString = "{0:C0}")]
[DataType(DataType.Currency)]
public float Cost { get; set; }
[Display(Name = @"Company")]
public string Company { get; set; }
[Display(Name = @"Code")]
public string Code { get; set; }
[Display(Name = @"Description")]
public string Description { get; set; }
[Display(Name = @"Mileage")]
public string Mileage { get; set; }
[Display(Name = @"Term")]
public string Term { get; set; }
public ProductCategoryItem()
{
Price = 0;
Cost = 0;
Company = "";
Code = "";
Description = "";
Mileage = "";
Term = "";
}
}
}
以下是部分视图:
@model ds_iDMS.Models.Deals.WorksheetAfterMarketViewModel
<div class="table_border_detail">
<h4 class="fex_section_header">Products</h4>
@using (Html.DSInputGroupRow(numberOfInputs: ExtensionMethods.NumberOfInputs.OneIput))
{
@Html.DSSelectFor(m => m.integratedProduct, new SelectList(Model.IntProducts, "Value", "Text", Model.integratedProduct))
}
@for (int i = 0; i < 10; i++)
{
using (Html.DSInputGroupRow(numberOfInputs: ExtensionMethods.NumberOfInputs.SevenInputs))
{
@Html.DSInputFor(m => m.ProductCategories[i].Price)
@Html.DSInputFor(m => m.ProductCategories[i].Cost)
@Html.DSSelectFor(m => m.ProductCategories[i].Company, new SelectList(Model.CompanyNames, "Value", "Text", Model.ProductCategories[i].Company))
@Html.DSInputFor(m => m.ProductCategories[i].Code)
@Html.DSInputFor(m => m.ProductCategories[i].Description)
@Html.DSInputFor(m => m.ProductCategories[i].Mileage)
@Html.DSInputFor(m => m.ProductCategories[i].Term)
}
}
</div>
以下是我们用于控制器的服务:
@model ds_iDMS.Models.Deals.WorksheetAfterMarketViewModel
<div class="table_border_detail">
<h4 class="fex_section_header">Products</h4>
@using (Html.DSInputGroupRow(numberOfInputs: ExtensionMethods.NumberOfInputs.OneIput))
{
@Html.DSSelectFor(m => m.integratedProduct, new SelectList(Model.IntProducts, "Value", "Text", Model.integratedProduct))
}
@for (int i = 0; i < 10; i++)
{
using (Html.DSInputGroupRow(numberOfInputs: ExtensionMethods.NumberOfInputs.SevenInputs))
{
@Html.DSInputFor(m => m.ProductCategories[i].Price)
@Html.DSInputFor(m => m.ProductCategories[i].Cost)
@Html.DSSelectFor(m => m.ProductCategories[i].Company, new SelectList(Model.CompanyNames, "Value", "Text", Model.ProductCategories[i].Company))
@Html.DSInputFor(m => m.ProductCategories[i].Code)
@Html.DSInputFor(m => m.ProductCategories[i].Description)
@Html.DSInputFor(m => m.ProductCategories[i].Mileage)
@Html.DSInputFor(m => m.ProductCategories[i].Term)
}
}
</div>
这是抛出错误时突出显示的行
@Html.DSSelectFor(m => m.ProductCategories[i].Company, new SelectList(Model.CompanyNames, "Value", "Text", Model.ProductCategories[i].Company))
我很丢失,不知道如何处理这个错误。如果您需要别的东西请告诉我