public ActionResult Save(WidgetVm widgetVm) { if (!ModelState.IsValid) { var vm = new WidgetVm { PlanLibCode = widgetVm.PlanLibCode, ItemTitle = widgetVm.ItemTitle, ItemDescription = widgetVm.ItemDescription, DefaultThreshold = widgetVm.DefaultThreshold, FormId = widgetVm.FormId, State = widgetVm.State, StateAbbr = widgetVm.StateAbbr, SiteId = widgetVm.SiteId, SiteName = widgetVm.SiteName }; return(View("AddNew", vm)); } if (Request.Url == null) { return(View("Current")); } var urlTest = Request.Url.AbsoluteUri; ItemsManager.Save(widgetVm, widgetVm.StateAbbr, urlTest, widgetVm.SiteId); return(RedirectToAction("Current", "Editor", new { stateAbbr = widgetVm.StateAbbr })); }
//save single PlanLibraryItem public static void Save(WidgetVm widgetVm, string stateAbbr, string urlTest, int?siteId = null) { try { using (var context = new LibraryItemsContext(stateAbbr, urlTest)) { var libraryItem = new PlanLibraryItem(); if (widgetVm.PlanLibCode == 0) { libraryItem = new PlanLibraryItem { ItemType = _itemType, ItemTypeID = _itemTypeId, ItemTitle = widgetVm.ItemTitle, ItemDescription = widgetVm.ItemDescription, DefaultThreshold = widgetVm.DefaultThreshold, MetricValueNote = _metricValueNote, PlanLibraryItemScopeID = _itemScopeId, FormID = widgetVm.FormId }; context.PlanLibraryItems.Add(libraryItem); } else { libraryItem = context.PlanLibraryItems.FirstOrDefault(i => i.PlanLibCode == widgetVm.PlanLibCode); if (libraryItem == null) { return; } libraryItem.ItemTitle = widgetVm.ItemTitle; libraryItem.ItemDescription = widgetVm.ItemDescription; libraryItem.DefaultThreshold = widgetVm.DefaultThreshold; libraryItem.FormID = widgetVm.FormId; } context.SaveChanges(); if (widgetVm.PlanLibCode != 0) { return; } var operatorId = context.Operators.FirstOrDefault(o => o.StateAbbr == stateAbbr).OperatorID; var scopeId = siteId; SaveCustomItemScope(libraryItem.PlanLibCode, scopeId, operatorId, context); } } catch (Exception ex) { throw ex; } }
//[Route("Editor/AddNew/{stateAbbr}")] public ActionResult AddNew(string stateAbbr) { if (string.IsNullOrEmpty(stateAbbr)) { return(RedirectToAction("Index", "Editor")); } if (Request.Url == null) { return(View("Current")); } var urlTest = Request.Url.AbsoluteUri; var widgetVm = new WidgetVm { State = StatesDictionary.States.FirstOrDefault(s => s.Key == stateAbbr).Value, StateAbbr = stateAbbr, Sites = ItemsManager.GetSites(stateAbbr, urlTest) }; ViewBag.Title = "Add New"; return(View(widgetVm)); }
//Get single PlanLibraryItem public static WidgetVm GetItem(string stateAbbr, string urlTest, int id) { var widgetVm = new WidgetVm(); try { using (var context = new LibraryItemsContext(stateAbbr, urlTest)) { var operatorId = context.Operators.FirstOrDefault(o => o.StateAbbr == stateAbbr).OperatorID; widgetVm = (from p in context.PlanLibraryItems .Where(p => p.PlanLibCode == id) from s in context.PlanLibraryItemsCustomFormScopes .Where(s => s.PlanLibCode == p.PlanLibCode && s.OperatorId == operatorId) from si in context.Sites .Where(si => si.SiteID == s.SiteId && si.OperatorID == operatorId) .DefaultIfEmpty() select new WidgetVm() { PlanLibCode = p.PlanLibCode, ItemTitle = p.ItemTitle, ItemDescription = p.ItemDescription, DefaultThreshold = p.DefaultThreshold, FormId = p.FormID, StateAbbr = stateAbbr, SiteId = si.SiteID, SiteName = si.SiteName }).FirstOrDefault(); } } catch (Exception ex) { throw ex; } widgetVm.State = StatesDictionary.States.FirstOrDefault(x => x.Key == stateAbbr).Value; return(widgetVm); }