[英]LINQ Entity Framework Value cannot be null. Parameter name: String
我试图从我的实体数据模型返回视图模型对象的集合到我的视图,并得到以下错误:
值不能为空。 参数名称:字符串 说明:在执行当前Web请求期间发生未处理的异常。 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: String
Source Error:
Line 22: {
Line 23: IEnumerable<CollegeContactViewModel> contacts = db.CollegeContacts.Where(cc => cc.CollegeContactID >0).AsEnumerable().ToList()
Line 24: .Select(c => new CollegeContactViewModel()
Line 25: {
Line 26: Id = c.CollegeContactID,
这是我的索引动作
public ActionResult Index()
{
IEnumerable<CollegeContactViewModel> contacts = db.CollegeContacts.AsEnumerable().ToList()
.Select(c => new CollegeContactViewModel()
{
Id = c.CollegeContactID,
Status = c.CollegeContStatus,
Center = c.CollegeContCenter,
Salutation = c.CollegeContSalutation,
FirstName = c.CollegeContFname,
LastName = c.CollegeContLname,
Position = c.CollegeContPosition,
Department = c.CollegeContDept,
Institution = c.CollegeContInstitution,
Address = new Address()
{
AddressLine1 = c.CollegeContAdd1,
AddressLine2 = c.CollegeContAdd2,
City = c.CollegeContCity,
State = c.CollegeContSt,
PostalCode = c.CollegeContZip
},
Email = c.CollegeContEmail,
Phone = c.CollegeContPhone,
Opt = c.CollegeContOpt,
ContactDate = c.CollegeContDate,
OnMailingList = c.CollegeContMailListSingle,
NumberOfCatalogsSent = int.Parse(c.NumCatalogsSent),
DateSent = c.Datesent.ToString(),
DateEntered = c.DateEntered.ToString(),
Reception = c.Reception,
Partner = c.Partner,
Website = c.SAwebsite,
Poster = c.CollegeContPoster
});
return View(contacts.ToList());
}
CollegeContactViewModel
public class CollegeContactViewModel
{
public int Id { get; set; }
public string Status { get; set; }
public string Center { get; set; }
public string Salutation { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Position { get; set; }
public string Department { get; set; }
public string Institution { get; set; }
public Address Address { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Opt { get; set; }
public string ContactDate { get; set; }
public bool? OnMailingList { get; set; }
public int NumberOfCatalogsSent { get; set; }
public string DateSent { get; set; }
public string DateEntered { get; set; }
public string Reception { get; set; }
public string Partner { get; set; }
public string Website { get; set; }
public string Poster { get; set; }
}
对于数据库中的另一个实体,我具有完全相同的代码类型,并且工作正常。 此外,除主键外,所有字段都允许为null,因此不能为空。 我也逐步浏览了代码,然后填充了ID。
有人可以告诉我为什么我不断收到此异常吗?
扫描您的代码中的字符串参数,这看起来像是最有可能(仅?)的候选者:
NumberOfCatalogsSent = int.Parse(c.NumCatalogsSent),
如果c.NumCatalogsSent
为null,则将得到错误。
我会改用这种方法(EF v4 可以转换null-coalescing运算符):
NumberOfCatalogsSent = int.Parse(c.NumCatalogsSent ?? "0"),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.