无法将类型int隐式转换为(namespace.model.class)类型

问题描述 投票:1回答:1

我有两个表service和Appointments,我试图将serviceId插入到Appointments表中,但是serviceId类型(Int)和约会。service.Id(namespace.model.class),所以错误是“无法隐式转换类型'int '至'namespace.model.class'

控制器:

 [HttpPost]
 ...
     if (ModelState.IsValid)
     {
         appointments = new Appointments()
         {
             service = appointments.service.Id,
         };

         db.Appointments.Add(appointments);
         db.SaveChanges();
     }
     return RedirectToAction("Index");
 }
c# asp.net-mvc
1个回答
0
投票

您正在尝试将对象分配给一个类型为int的ID。如果您在Appointments类中有外键,例如将ServiceId分配给它。

appointments = new Appointments()
         {
             serviceid = appointments.service.Id,
         };

或者,如果您没有该属性,则应该这样做

appointments = new Appointments()
         {
             service = appointments.service,
         };

但是您的appointment.service类不应为null。

© www.soinside.com 2019 - 2024. All rights reserved.