public ActionResult Create() { ModulesViewModel model = new ModulesViewModel() { DataTypeList = GetDataTypeList() }; return View(model); }
public ActionResult Create(ModulesViewModel _model) { if (!ModelState.IsValid) { _model.DataTypeList = GetDataTypeList(); return View(_model); } if(_model.Fields.Count(f => f.ShowInListMode) == 0) { TempData["Message"] = "Pelo menos um dos campos deve ser visível na listagem."; _model.DataTypeList = GetDataTypeList(); return View(_model); } // retrieve the current account name and associate with the newly created module var acctName = RouteData.Values["account"] as string; if (String.IsNullOrWhiteSpace(acctName)) { TempData["Message"] = "Conta não encontrada."; return View(_model); } Module newModule = new Module() { DisplayName = _model.DisplayName, ModuleName = _model.DisplayName.EscapeName(), User = ModRepository.GetUserByName(Membership.GetUser().UserName), // associate the module with the current logged in user Account = ModRepository.GetAccountByName(acctName), ModuleType = _model.Type }; _model.Fields.ToList().ForEach(f => { var newField = new Field { DisplayName = f.FieldDisplayName, FieldName = f.FieldDisplayName.EscapeName(), FieldDataType = ModRepository.GetDataTypeByName(f.DataType), IsReadOnly = f.IsReadOnly, IsRequired = f.IsRequired, ShowInListMode = f.ShowInListMode }; if (f.Metadata != null) { newField.AddMetadata(f.Metadata); } newModule.Fields.Add(newField); }); ModRepository.AddModule(newModule); TempData["Message"] = "Módulo salvo com sucesso."; return RedirectToAction("Index", "Home"); }
public ActionResult Edit(ModulesViewModel _model) { if (!ModelState.IsValid) { _model.DataTypeList = GetDataTypeList(); return View(_model); } var modToEdit = ModRepository.GetModuleById(_model.Id, m => m.Fields.Include<Field, FieldDataType>(f => f.FieldDataType), m => m.Fields.Include<Field, FieldMetadata>(f => f.Metadata), m => m.Rows.Include<Row, Cell>(r => r.Cells)); if (modToEdit == null) { TempData["Message"] = "Módulo não encontrado."; _model.DataTypeList = GetDataTypeList(); return View(_model); } modToEdit.DisplayName = _model.DisplayName; if (_model.Fields.Count(f => f.ShowInListMode) == 0) { TempData["Message"] = "Pelo menos um dos campos deve ser visível na listagem."; _model.DataTypeList = GetDataTypeList(); return View(_model); } // handle modified and added fields foreach (ModuleField f in _model.Fields) { Field dbField = modToEdit.Fields.FirstOrDefault(fld => fld.ID == f.ID); bool bNeedToAdd = (dbField == null); if (dbField == null) { dbField = new Field(); } if (!bNeedToAdd) { dbField.FieldDataTypeReference.Load(); dbField.Metadata.Load(); if (dbField.FieldDataType.Name != f.DataType) { // field type changed. set it to null on all matching rows modToEdit.Rows.ToList().ForEach(r => { r.Cells.Load(); r.Cells .ToList() .ForEach(rf => { rf.FieldReference.Load(); if (rf.Field.FieldName == dbField.FieldName) { rf.Data = null; } }); }); } } dbField.DisplayName = f.FieldDisplayName; dbField.FieldName = f.FieldDisplayName.EscapeName(); dbField.FieldDataType = ModRepository.GetDataTypeByName(f.DataType); dbField.IsReadOnly = f.IsReadOnly; dbField.IsRequired = f.IsRequired; dbField.ShowInListMode = f.ShowInListMode; dbField.Module = modToEdit as Module; if (f.Metadata != null) { dbField.AddMetadata(f.Metadata); } if (bNeedToAdd) { ModRepository.AddField(dbField); modToEdit.Rows.ToList() .ForEach(row => { Cell rf = new Cell() { Field = dbField, Row = row }; ModRepository.AddCell(rf); }); } } ModRepository.Save(); TempData["Message"] = "Módulo salvo com sucesso."; return RedirectToAction("List", "DynamicModule", new { moduleid = _model.Id, modulename = modToEdit.ModuleName }); }
public ActionResult Edit(int id) { var modToEdit = ModRepository.GetModuleById(id, m => m.Fields.Include<Field, FieldDataType>(f => f.FieldDataType), m => m.Fields.Include<Field, FieldMetadata>(f => f.Metadata)); if (modToEdit == null) { TempData["Message"] = "Módulo não encontrado."; return RedirectToAction("Index", "Home"); } ModulesViewModel model = new ModulesViewModel() { Id = modToEdit.Id, DisplayName = modToEdit.DisplayName, DataTypeList = GetDataTypeList(), Type = modToEdit.ModuleType }; model.Fields.AddRange( modToEdit.Fields.ToList().Select(f => new ModuleField { ID = f.ID, IsRequired = f.IsRequired, IsReadOnly = f.IsReadOnly, DataType = f.FieldDataType.Name, FieldDisplayName = f.DisplayName, ShowInListMode = f.ShowInListMode, Metadata = f.Metadata.FirstOrDefault() != null ? f.Metadata.FirstOrDefault().Value : null // *1 })); // *1 TODO: Despite the field-metadata relationship is 1-N, we're passing only the first entry to the view for now. // This will probably change in the future when we have multiple metadata entries for each field. return View(model); }