[英]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.