0

I don`t understand how to do it. I have three models.

My supplier model

public class Supplier
{
    [Key]
    public int SupplierID { set; get; }
    public string Name { set; get; }
    public virtual ICollection<Manager> Managers { get; set; }
}

Supplier have many managers.

My manager model

public class Manager
{
    [Key]
    public int ManagerID { set; get; 
    public string Name { set; get; }
    public virtual ICollection<Supplier> Suppliers { get; set; }
    public virtual ICollection<Phone> Phones { get; set; }
}

Managers has many phone numbers

Phone model

public class Phone
{
    [Key]
    public int PhoneID { set; get; }
    public string PhoneNumber { set; get; }
    public virtual ICollection<Manager> Manager { get; set; }
}

Then I create ModelView in my project

public class SupplierView
{
    public Supplier Supplier { get; set; }
    public IEnumerable<Manager> Managers { get; set; }
    public string Manger_Name { get; set; }
    public string Manager_New_Phone { get; set; }
}

In controller I create model and pass model to partial View.

public ActionResult Edit(int id)
{
    var supplier = db.Suppliers.Find(id);
    var model = new SupplierView
    {
        Supplier = supplier
    };

    return PartialView("_Supplier_Edit", model);
}

Edit View works good, show all information about supplier.

@using (Html.BeginForm("Save", "Supplier", FormMethod.Post, new { @class = "ui form" }))
{
    @Html.AntiForgeryToken()
        <div class="two fields">
        <div class="field">
            @Html.LabelFor(m => Model.Supplier.Name)
        </div>
    </div>
    foreach (var manager in Model.Supplier.Managers)
    {
        <div class="two fields">
            <div class="field">
                <label>Manager Name</label>

                @if (manager.Name.Any())
                {
                    @Html.TextBoxFor(m => manager.Name)
                }
                else
                {
                    @Html.TextBoxFor(m => m.Manger_Name)
                }
            </div>
            <div class="field">
                <label>Phone</label>
                @if (manager.Phones.Any())
                {
                    foreach (var phones in manager.Phones)
                    {
                        @Html.TextBoxFor(m => phones.PhoneNumber)
                    }
                }
                else
                {
                    @Html.TextBoxFor(m => m.Manager_New_Phone)
                }
            </div>
        </div>
    }
}

I don`t know how to do save controller for that form.

Details controller

[HttpPost]
public ActionResult Details(int id)
{
    var supplier = db.Suppliers.Find(id);
    if (supplier == null)
    {
        return HttpNotFound();
    }
    var model = new SupplierView
    {
         Supplier = supplier
    };
    return PartialView("_Supplier_Details", model);
}
Shato
  • 43
  • 6

0 Answers0