2012-11-15 53 views
1

我試圖視圖模型對象的集合返回我的觀點,從我的實體數據模型,並得到了以下錯誤:LINQ實體框架值不能爲空。參數名:字符串

Value cannot be null. Parameter name: String Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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正在填充。

有人可以告訴我爲什麼我不斷得到這個異常?

+1

快速瀏覽它可能是你的Int.Parse? –

+0

你釘了它。謝謝......我不能相信我錯過了這一點。我會在一分鐘內將你的答案標記爲答案。謝謝:) –

回答

1

掃描您的代碼字符串參數,這看起來像是最有可能的(只?)候選人:

NumberOfCatalogsSent = int.Parse(c.NumCatalogsSent), 

如果c.NumCatalogsSent爲空,這是你會得到錯誤。


我想嘗試這個代替(EF V4 可以翻譯空合併運算符):

NumberOfCatalogsSent = int.Parse(c.NumCatalogsSent ?? "0"), 
+0

再次感謝... :) –

相關問題