Код: Выделить всё
public class Facture
{
public int Id { get; set; }
[ValidateNever]
[DataType(DataType.Date)]
public DateTime? DateCreation { get; set; }
[Required]
[DataType(DataType.Date)]
[Display(Name = "Date de facturation")]
public DateTime DateFacturation { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Date d'échéance")]
public DateTime? DateEcheance { get; set; }
[ValidateNever]
public required Client Client { get; set; }
}
public class Client
{
public int Id { get; set; }
[Required]
public required string Name { get; set; }
ICollection? Factures { get; set; }
}
Код: Выделить всё
public IActionResult Create()
{
ViewBag.Client = new SelectList(_context.Client, "Id", "Name");
return View();
}
Код: Выделить всё
Код: Выделить всё
public async Task Create([Bind("Id,DateFacturation,DateEcheance,Client")] Facture facture)
{
if (ModelState.IsValid)
{
facture.DateCreation = DateTime.Now;
_context.Add(facture);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(facture);
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... oreign-key
Мобильная версия