Getting error on POST with Entity Framework - Value cannot be null. Parameter name: source

前端 未结 2 603
闹比i
闹比i 2021-01-24 05:20

EDIT - as requested, this is the view...

--start edit

@model salesWebTest.viewModel.vwbooking

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-24 06:03

    Your not checking if vwbooking.traces == null before calling it. While you might expect that .ToList() can protect you from this, Entity Framework can be quirky (anecdotal). Safeguard your call with

    if (ModelState.IsValid)
    {
    
        db.bookings.Attach(vwbooking.bookings);
        db.Entry(vwbooking.bookings).State = EntityState.Modified;
        if(vwbooking.traces != null)
        {
                vwbooking.traces.ToList().ForEach( //THE ERROR OCCURS HERE
                t =>
                {
                db.traces.Attach(t);
                db.Entry(t).State = EntityState.Modified;
                }
            );
            db.SaveChanges();
        }
    }
    

    and you should be fine.

提交回复
热议问题