public ActionResult Create(FormCollection collection) { ViewBag.ServiceSuccess = "The service has been successfuly added"; var db = new DBcontext(); var serviceServiceTypeViewModel = new ServiceServiceTypeViewModel(); serviceServiceTypeViewModel.ServiceTypes = db.ServiceTypes; try { Service service = new Service { Rate = Convert.ToDecimal(Request.Form["Service.Rate"]), BeginDate = Convert.ToDateTime(Request.Form["BeginDate"]), EndDate = Convert.ToDateTime(Request.Form["EndDate"]), ServiceTypeId = Convert.ToInt16(Request.Form["Service.ServiceTypeId"]), ClientId = Convert.ToInt16(Request.Form["Service.ClientID"]) }; if (db.Clients.Single(c => c.ID == service.ClientId).Services == null) { db.Clients.Single(c => c.ID == service.ClientId).Services = new List <Service>(); } db.Clients.Single(c => c.ID == service.ClientId).Services.Add(service); db.Services.Add(service); db.SaveChanges(); return(View(serviceServiceTypeViewModel)); } catch { return(View()); } }
// GET: Service/Create/id public ActionResult Create(int id) { var db = new DBcontext(); ViewBag.ClientId = id; var serviceServiceTypeViewModel = new ServiceServiceTypeViewModel(); serviceServiceTypeViewModel.ServiceTypes = db.ServiceTypes; return(View(serviceServiceTypeViewModel)); }