0

I am trying to achieve a many to many relationship in a MVC app where in I have a Billing Model and the Billing Model contains Many Services.

I have Achieved One-to-One and One-to-Many Relationship in my App but I don't know how to achieve many to many relationship in Create View

Billing Model :

public class Billing
    {
        public Billing()
        {
            this.Services = new HashSet<Services>();

        }

        [Key]
        public int BillingsID { get; set; }

        [Required]
        public int EmployeesID { get; set; }
        public virtual Employees Employeee { get; set; }

        [Required]
        public virtual ICollection<Services> Services { get; set; }

    }

Services Model :

public class Services
    {
        public Services()
        {
            this.Billings = new HashSet<Billing>();
        }
        [Key]
        public int ServicesID { get; set; }

        [Display(Name = "Name")]
        [Required(AllowEmptyStrings = false)]
        public string Title { get; set; }

        [Display(Name = "Price")]
        [DataType(DataType.Currency)]
        [Required]
        public float price { get; set; }

        public virtual ICollection<Billing> Billings { get; set; }
        public virtual String FullName { get { return Title + "- [" + price+"]"; } }
    }

I want to Insert multiple Objects of Services corresponding to a single Billing Object. Some thing like an ADD button which when clicked will give an option to select a Service from the List.

rvsingh42
  • 71
  • 1
  • 10
  • As you have mentioned "I want to Insert multiple Objects of Services corresponding to a single Billing Object" It's One-to-Many Relationship Not many-to-many – Basanta Matia Jun 28 '17 at 18:10
  • Refer [this answer](https://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for some options, and [this one](https://stackoverflow.com/questions/40539321/a-partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892#40541892) for a more detailed example using `BeginCollectionItem` –  Jun 28 '17 at 22:14

0 Answers0