1

ビュー モデル オブジェクトのコレクションをエンティティ データ モデルからビューに返そうとすると、次のエラーが発生します。

値を null にすることはできません。パラメーター名: 文字列 説明: 現在の 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 が入力されています。

この例外が発生し続ける理由を教えてください。

4

1 に答える 1

2

文字列パラメーターのコードをスキャンすると、これが最も可能性の高い (唯一の?) 候補のように見えます。

NumberOfCatalogsSent = int.Parse(c.NumCatalogsSent),

null の場合c.NumCatalogsSent、それがエラーになります。


代わりにこれを試してみます(EF v4はnull合体演算子を変換できます):

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