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

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

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

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

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

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

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

            if (this.ModelState.IsValid)
            {
                lab.Name    = model.Name;
                lab.Address = model.Address;
                var phoneId = lab.Phones.Any() ? lab.Phones.Max(x => x.PhoneId) + 1 : 1;
                lab.Phones.Clear();
                if (model.Phones != null)
                {
                    foreach (var phone in model.Phones.Split(',').Select(x => x.Trim()).Where(x => !String.IsNullOrEmpty(x)))
                    {
                        lab.Phones.Add(new LabPhone
                        {
                            PhoneId = phoneId++,
                            Number  = phone
                        });
                    }
                }

                await this.context.SaveChangesAsync();

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

            return(this.View(model));
        }