// GET: Hospitals/Edit/5
        public async Task <IActionResult> Edit(Int32?id)
        {
            if (id == null)
            {
                return(this.NotFound());
            }

            var hospital = await this.context.Hospitals
                           .Include(x => x.Phones)
                           .SingleOrDefaultAsync(m => m.Id == id);

            if (hospital == null)
            {
                return(this.NotFound());
            }

            var model = new HospitalEditModel
            {
                Name    = hospital.Name,
                Address = hospital.Address,
                Phones  = String.Join(", ", hospital.Phones.OrderBy(x => x.PhoneId).Select(x => x.Number))
            };

            return(this.View(model));
        }
        public async Task <IActionResult> Edit(Int32?id, HospitalEditModel model)
        {
            if (id == null)
            {
                return(this.NotFound());
            }

            var hospital = await this.context.Hospitals
                           .Include(x => x.Phones)
                           .SingleOrDefaultAsync(m => m.Id == id);

            if (hospital == null)
            {
                return(this.NotFound());
            }

            if (this.ModelState.IsValid)
            {
                hospital.Name    = model.Name;
                hospital.Address = model.Address;
                var phoneId = hospital.Phones.Any() ? hospital.Phones.Max(x => x.PhoneId) + 1 : 1;
                hospital.Phones.Clear();

                if (model.Phones != null)
                {
                    foreach (var phone in model.Phones.Split(',').Select(x => x.Trim()).Where(x => !String.IsNullOrEmpty(x)))
                    {
                        hospital.Phones.Add(new HospitalPhone
                        {
                            PhoneId = phoneId++,
                            Number  = phone
                        });
                    }
                }

                await this.context.SaveChangesAsync();

                return(this.RedirectToAction("Index"));
            }

            return(this.View(model));
        }