Как добавить еще одну позицию к уже существующему порядку в контроллере MVC?
public class Order { [Key] public int IdOrder { get; set; } public string UserId { get; set; } public virtual User User { get; set; } public int IdOrderAttachment { get; set; } public virtual OrderAttachment OrderAttachment { get; set; } public virtual ICollection<Employee> Employee { get; set; } [Required(ErrorMessage = "Specify the date of order acceptance")] [Display(Name = "Date of acceptance of the order")] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTimeDateOfAcceptance { get; set; } [Display(Name = "Date of completion planning")] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime? DateOfCompletionPlanning { get; set; } [Display(Name = "End Date")] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime? EndDate { get; set; } [Required(ErrorMessage = "Enter the subject")] [MaxLength(200, ErrorMessage = "Name max 200 characters")] [Display(Name = "Subject")] public string Subject { get; set; } //many to many public virtual ICollection<Position> PositionList { get; set; } } public class Position { [Key] public int IdPosition { get; set; } [Column(TypeName = "nvarchar(MAX)")] [Display(Name = "Description")] [UIHint("tinymce_jquery_full"), AllowHtml] public string Description { get; set; } public virtual ICollection<OrderPosition> OrderList { get; set; } } //Map many to many base.OnModelCreating(modelBuilder); modelBuilder.Entity<Order>() .HasMany<Position>(s => s.PositionList) .WithMany(c => c.OrderList) .Map(cs => { cs.ToTable("OrderPosition"); cs.MapLeftKey("IdOrder"); cs.MapRightKey("IdPosition"); });
Что я уже пробовал:
[HttpPost] [ValidateAntiForgeryToken] public PartialViewResult _AddPost(int IdOrder) { var findOrder = db.Order.Find(IdOrder); if (ModelState.IsValid) { Position position = new Position { Description = "Test"}; db.Position.Attach(position); findOrder.PositionList.Add(position); db.SaveChanges(); } return PartialView(); }
Я пробовал, как и выше, но это не работает