我要更新用户记录后,当我按“更新”按钮时,我得到
An unhandled exception occurred while processing the request.
ArgumentNullException: Value cannot be null.
Parameter name: source
System.Linq.Enumerable.Select<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, TResult> selector)
这是我更新用户记录的功能:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, AppointmentDetailsViewModel objAppointmentVM)
{
if(ModelState.IsValid)
{
objAppointmentVM.Appointment.AppointmentDate = objAppointmentVM.Appointment.AppointmentDate
.AddHours(objAppointmentVM.Appointment.AppointmentTime.Hour)
.AddMinutes(objAppointmentVM.Appointment.AppointmentTime.Minute);
var appointmentFromDb = _db.Appointments.Where(a => a.Id == objAppointmentVM.Appointment.Id).FirstOrDefault();
appointmentFromDb.CustomerName = objAppointmentVM.Appointment.CustomerName;
appointmentFromDb.CustomerEmail = objAppointmentVM.Appointment.CustomerEmail;
appointmentFromDb.CustomerPhoneNumber = objAppointmentVM.Appointment.CustomerPhoneNumber;
appointmentFromDb.AppointmentDate = objAppointmentVM.Appointment.AppointmentDate;
appointmentFromDb.isConfirmed = objAppointmentVM.Appointment.isConfirmed;
if(User.IsInRole(SD.SuperAdminEndUser))
{
appointmentFromDb.SalesPersonId = objAppointmentVM.Appointment.SalesPersonId;
}
_db.SaveChanges();
return RedirectToAction(nameof(Index));
}
return View(objAppointmentVM);
}
此功能适用于下拉菜单,可显示来自数据库的用户
public static IEnumerable<SelectListItem> ToSelectListItemString<T>(this IEnumerable<T> items, string selectedValue)
{
if(selectedValue == null)
{
selectedValue = "";
}
return from item in items
select new SelectListItem
{
Text = item.GetPropertyValue("Name"),
Value = item.GetPropertyValue("Id"),
Selected = item.GetPropertyValue("Id").Equals(selectedValue.ToString())
};
}
任何想法我弄错了吗?
答案 0 :(得分:0)
也许您表的列设置为not null
。
转到SQL Server并打开表。在设计表窗口中,选中allow nulls
复选框。
答案 1 :(得分:0)
在您的项目中,选择其中一项为null,并且LINQ未能引发参数异常,因为您试图在null值上运行选择器函数。