public ActionResult CreateView(CreateViewModel model) { DataViewDataAccess dataAccess = new DataViewDataAccess(); if (!dataAccess.isCreateViewModelValid(model)) { ModelState.AddModelError("ViewName", "The View Name must be unique"); CreateViewModel tempModel = dataAccess.getDefaultCreateViewModel(); model.GeographyTypes = tempModel.GeographyTypes; model.NewColumnModel = dataAccess.getDefaultColumnModel(); List<Breadcrumb> trail = new List<Breadcrumb>(); trail.Add(new Breadcrumb() { LinkText = "Home", Action = "Index", Controller = "Home", isCurrent = false }); trail.Add(new Breadcrumb() { LinkText = "Data View Index", Action = "Index", Controller = "DataView", isCurrent = false }); trail.Add(new Breadcrumb() { LinkText = "Create Data View", Action = "", Controller = "", isCurrent = true }); model.Breadcrumbs = trail; return View(model); } try { dataAccess.CreateView(model); } catch (SqlException) { ModelState.AddModelError("", "An error occured when creating the Data View. Please ensure that the Data View Name is unique and that the columns are uniquely named within it."); CreateViewModel tempModel = dataAccess.getDefaultCreateViewModel(); model.GeographyTypes = tempModel.GeographyTypes; model.NewColumnModel = dataAccess.getDefaultColumnModel(); List<Breadcrumb> trail = new List<Breadcrumb>(); trail.Add(new Breadcrumb() { LinkText = "Home", Action = "Index", Controller = "Home", isCurrent = false }); trail.Add(new Breadcrumb() { LinkText = "Data View Index", Action = "Index", Controller = "DataView", isCurrent = false }); trail.Add(new Breadcrumb() { LinkText = "Create Data View", Action = "", Controller = "", isCurrent = true }); model.Breadcrumbs = trail; return View(model); } TempData["SuccessMessage"] = String.Format("The Data View - {0}, was successfully created", model.ViewName); return RedirectToAction("Index"); }
/// <summary> /// A method to create the View from the user entered data that /// has been specified through the web interface. It uses the template /// for a new View to generate the necessary SQL. /// </summary> /// <param name="model">The fully created model</param> public void CreateView(CreateViewModel model) { Geographical_NeedsEntities context = new Geographical_NeedsEntities(); DataView newView = context.DataViews.Create(); newView.ViewName = model.ViewName; DataViewColumn geogColumn = context.DataViewColumns.Create(); geogColumn.ColumnName = context.GeographyTypes.Single(x => x.GeographyTypeID.Equals(model.SelectedGeographyType)).GeographyType1; geogColumn.DataView = newView; context.DataViewColumns.Add(geogColumn); foreach (var column in model.Columns) { DataViewColumn newColumn = context.DataViewColumns.Create(); newColumn.ColumnName = column.ColumnName; newColumn.DataView = newView; context.DataViewColumns.Add(newColumn); } context.DataViews.Add(newView); CreateViewTemplate template = new CreateViewTemplate(); template.Model = model; String output = template.TransformText(); context.Database.ExecuteSqlCommand(output); context.SaveChanges(); context.Dispose(); }
/// <summary> /// Validate that the Create Data View Model is valid prior to /// submission to the database /// </summary> /// <param name="model">The model to be validated</param> /// <returns>Boolean indicating valid or not</retu public Boolean isCreateViewModelValid(CreateViewModel model) { Geographical_NeedsEntities context = new Geographical_NeedsEntities(); int count = context.DataViews.Where(x => x.ViewName.Equals(model.ViewName)).Count(); return count == 0; }
/// <summary> /// A method to get the basic Create View model with drop down /// data populated /// </summary> /// <returns>The populated model</returns> public CreateViewModel getDefaultCreateViewModel() { CreateViewModel model = new CreateViewModel(); Geographical_NeedsEntities context = new Geographical_NeedsEntities(); model.GeographyTypes = context.GeographyTypes.Select(x => new SelectListItem() { Text = x.GeographyType1, Value = x.GeographyTypeID.ToString() }); return model; }