Example #1
0
        public async Task <IActionResult> Create(Int32?hospitalId, WardCreateModel model)
        {
            if (hospitalId == null)
            {
                return(this.NotFound());
            }

            var hospital = await this.context.Hospitals
                           .SingleOrDefaultAsync(x => x.Id == hospitalId);

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

            if (this.ModelState.IsValid)
            {
                var ward = new Ward
                {
                    HospitalId = hospital.Id,
                    Name       = model.Name
                };

                this.context.Add(ward);
                await this.context.SaveChangesAsync();

                return(this.RedirectToAction("Index", new { hospitalId = hospital.Id }));
            }

            this.ViewBag.Hospital = hospital;
            return(this.View(model));
        }
        public async Task <IActionResult> Create(Int32?hospitalId, WardCreateModel model)
        {
            if (hospitalId == null)
            {
                return(this.NotFound());
            }

            var hospital = await this.context.Hospitals
                           .SingleOrDefaultAsync(x => x.Id == hospitalId);

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

            if (this.ModelState.IsValid)
            {
                var ward = new Ward
                {
                    HospitalId = hospital.Id,
                    Name       = model.Name,
                    WardStaff  = new Collection <WardStaff>()
                };


                if (model.wardStaff != null)
                {
                    var staffId = 1;
                    foreach (var staff in model.wardStaff)
                    {
                        ward.WardStaff.Add(new WardStaff
                        {
                            StaffId  = staffId++,
                            Name     = staff.Name,
                            Position = staff.Position
                        });
                    }
                }



                this.context.Add(ward);
                await this.context.SaveChangesAsync();

                return(this.RedirectToAction("Index", new { hospitalId = hospital.Id }));
            }

            this.ViewBag.Hospital = hospital;
            return(this.View(model));
        }