我在网上找不到任何东西。我有一个ReservationDate
模型,
namespace Mijem_test_app.Models
{
[HandleError]
/*
This model includes the location,
the person who reserved it and
FOR when they reserved it (not
when they reserved it)
*/
public class ReservationDate
{
//key
public int Id { get; set; }
//location name
[Required]
public Reservation Reservation { get; set; }
//contact
[Required]
public Contact Contact { get; set; }
//date reserved for
[Required]
public DateTime ReservedDate { get; set; }
//information from textbox
[Required]
public string InfoFromTextBox { get; set; }
public bool Deleted { get; set; }
public ImagesUploaded ImageURL { get; set; }
}
}
在我的控制器中,我尝试记录CREATE
,
[HttpPost]
public ActionResult Save(ReservationDate reservation)
{
var _contactID = (int) TempData["ContactID"];
var _contact = _context.Contacts
.Single(r => r.Id == _contactID);
var _location = _context.Reservations
.Single(r => r.Name == reservation.Reservation.Name);
var _ReservedDate = (DateTime)TempData["BookDate"];
var _InfoFromTextBox = (string)TempData["InfoFromTextBox"];
var __reservation = new ReservationDate
{
ReservedDate = _ReservedDate,
Contact = _contact.Id,
Reservation = _location.Id,
InfoFromTextBox = _InfoFromTextBox,
Deleted = false
};
_context.ReservationDates.Add(__reservation);
_context.SaveChanges();
return View("Index");
}
代码在
处中断 Contact = _contact.Id,
Reservation = _location.Id,
,调试器分别告诉我Cannot implicitly convert type 'int' to 'app.Models.Contact
和Cannot implicitly convert type 'int' to 'app.Models.Reservation
。我究竟做错了什么?我的Reservation
和Contact
模型是
namespace app.Models
{
[HandleError]
public class Reservation
{
//unique ID of location
public int Id { get; set; }
//name of location
[Required]
[StringLength(255)]
public string Name { get; set; }
public int Ranking { get; set; }
public bool Favorites { get; set; }
}
}
和
namespace app.Models
{
[HandleError]
public class Contact
{
public int Id { get; set; }
[Required]
[StringLength(255)]
[Display(Name = "Contact Name ...")]
public string Name { get; set; }
[Required]
[StringLength(255)]
[Display(Name = "Contact Type")]
public string ContactType { get; set; }
[Required]
[Display(Name = "Phone:")]
public long ContactNumber { get; set; }
[Column(TypeName = "date")]
[Display(Name = "Birthdate")]
public DateTime BirthDate { get; set; }
}
}
我已经尝试了所有方法。.我想我可能不正确地使用LINQ?我在这里茫然。
答案 0 :(得分:1)
在ReservationDate类中,您需要具有Contact和Reservation表的导航属性。 像这样添加 ContactId 和 ReservationId 属性
public class ReservationDate
{
//key
public int Id { get; set; }
public int ReservationId { get; set; } // this
//location name
[Required]
public Reservation Reservation { get; set; }
public int ContactId { get; set; } // this
//contact
[Required]
public Contact Contact { get; set; }
//date reserved for
[Required]
public DateTime ReservedDate { get; set; }
//information from textbox
[Required]
public string InfoFromTextBox { get; set; }
public bool Deleted { get; set; }
public ImagesUploaded ImageURL { get; set; }
}
MVC自动将该 ContactID FK绑定到联系人表PK ID 。
在保存时,您呼叫 ContactId 而不是像这样的 Contact 对象
ContactId = _contact.Id