1

我正在尝试从我的 Entity 数据模型将视图模型对象的集合返回到我的视图并得到以下错误:

值不能为空。参数名称:字符串 描述:当前网络请求执行过程中发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

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());
        }

学院联系视图模型

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; }
    }

我的数据库中的另一个实体有完全相同类型的代码,它工作得很好。此外,除主键外,所有字段都允许为空,因此不可能。我还单步执行了代码并且 Id 正在被填充。

有人可以告诉我为什么我不断收到这个例外吗?

4

1 回答 1

2

扫描您的代码以查找字符串参数,这看起来最有可能(仅?)候选:

NumberOfCatalogsSent = int.Parse(c.NumCatalogsSent),

如果c.NumCatalogsSent为空,那就是你会得到的错误。


我会尝试这个(EF v4可以翻译 null-coalescing 运算符):

NumberOfCatalogsSent = int.Parse(c.NumCatalogsSent ?? "0"),
于 2012-11-15T07:58:13.930 回答