Not the first day I struggle with the problem of creating a form for a one-to-many relationship.

There are directors (I try to shorten the code as much as possible).

public class Supplier { [Display(Name = "SupplierID")] [Key] public int SupplierID { set; get; } [Display(Name = "Коммерческое название")] [StringLength(250)] [Column(TypeName = "char")] public string Name { set; get; } public virtual ICollection<Manager> Managers { get; set; } } 

Stage directors can have many managers. Class managers.

 public class Manager { [Display(Name = "ManagerID")] [Key] public int ManagerID { set; get; } [Display(Name = "ФИО")] [StringLength(250)] [Column(TypeName = "char")] public string Name { set; get; } public virtual ICollection<Phone> Phones { get; set; } } 

Managers can have many phone numbers.

 public class Phone { [Display(Name = "PhoneID")] [Key] public int PhoneID { set; get; } [Display(Name = "Номер Телефона")] [StringLength(250)] [Column(TypeName = "char")] public string PhoneNumber { set; get; } } 

Based on these three classes, I created a base.

For display I created ModelView (please do not pay attention to services and selects, it all works and does not cause any issues)

 public class SupplierDetailView { public Supplier Supplier { get; set; } public int[] SelectedServicesIds { get; set; } public IEnumerable<Service> Services { get; set; } public IEnumerable<Manager> Managers { get; set; } } 

To edit the Director, I created a controller. This is where the problems begin.

 [HttpPost] public ActionResult Edit(int id) { var supplier = db.Suppliers.Find(id); var managers = db.Managers; var services = db.Services.Select(s => new { ServiceID = s.ServiceID, Service_Name = s.Service_Name }).ToList(); var supplier_services = supplier.Services; ViewBag.Services = new MultiSelectList(services, "ServiceID", "Service_Name", supplier_services.Select(s => s.ServiceID).ToArray()); var model = new SupplierDetailView { Supplier = supplier, Managers = managers }; return PartialView("_Supplier_Edit", model); } 

The problem begins when you try to display all of this in View.

1) How can I display data in TextBox if it doesn’t exist?

 @model Jumper.ViewModel.SupplierDetailView @using (Html.BeginForm("Save", "Supplier", FormMethod.Post, new { @class = "ui form" })) { <div class="two fields"> <div class="field"> @Html.LabelFor(m => Model.Supplier.ShortName) @Html.TextBoxFor(m => m.Supplier.ShortName) </div> <div class="field"> @Html.LabelFor(m => Model.Supplier.Name) @Html.TextBoxFor(m => m.Supplier.Name) </div> </div> </div> 

For example, according to the director in the form there is no ShortName data, therefore the form will not display this field. I need to display. I can not understand how to display Managers in this form. I do it like this.

 foreach (var manager in Model.Supplier.Managers) { <div class="two fields"> <div class="field"> <label>ФИО</label> @Html.TextBoxFor(m => manager.Name) </div> <div class="field"></div> </div> <div class="two fields"> <div class="field"> <label>Телефон</label> @foreach (var phone in manager.Phones) { @Html.TextBoxFor(m => phone.PhoneNumber) } </div> 

2) Here is the same problem. If the name is, but there is no phone, then the field for editing or adding phone data will not be displayed. How can I do it?

1 answer 1

For example, according to the director in the form there is no ShortName data, therefore the form will not display this field. I need to display. I can not understand how to display Managers in this form.

You can use this relatively painless way:

 @Html.TextBoxFor(m => m.Supplier.ShortName, new { @Value = Model.Supplier.ShortName ?? "какое-то значение по умолчанию" }) 

Here is the same problem. If the name is, but there is no phone, then the field for editing or adding phone data will not be displayed. How can I do it?

Here you can add a little server logic:

 @if(Model.Phones.Any()) { @foreach (var phone in manager.Phones) { @Html.TextBoxFor(m => phone.PhoneNumber) } } else { @Html.TextBoxFor(m => m.NewPhoneNumber) } 

and define the NewPhoneNumber property in the model, which will be used if the Phones collection is empty.

  • and define the NewPhoneNumber property in the model, which will be used if the Phones collection is empty. Please, can you give an example? - shatoidil