如何在控制器MVC中为现有订单添加另一个位置? [英] How to add another position to the already existing order in controller MVC?

查看:58
本文介绍了如何在控制器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");
                       });





我尝试了什么:





What I have tried:

[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();
               }





我尝试过如上但不起作用



I tried as above but it does not work

推荐答案

db.Position.Attach(position);





更改





change on

<pre>db.Position.Add(position);





很棒的工作。



Great work.


这篇关于如何在控制器MVC中为现有订单添加另一个位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆