//przeładowane metody w celu dodaniapytania od razu do formularza public IActionResult AddNewField(int?id) { ViewBag.formid = Convert.ToInt32(id); NewFieldList newFieldList = new NewFieldList(); var idFieldsInForm = _context.FormField.Where(ff => ff.IdForm == id) .Select(ff => ff.IdField).ToList(); var fieldsInForm = _context.Field.Where(f => idFieldsInForm.Contains(f.Id)).ToList(); List <FieldWithValidation> fieldWithValidations = new List <FieldWithValidation>(); foreach (var item in fieldsInForm) { FieldWithValidation pom = new FieldWithValidation { Id = item.Id, Name = item.Name, Type = item.Type }; fieldWithValidations.Add(pom); } newFieldList.fields = fieldWithValidations; newFieldList.FormId = Convert.ToInt32(id); return(View(newFieldList)); }
public IActionResult AddToList(NewFieldList newFieldList) { FieldWithValidation field; if (newFieldList.currentName == "") { return(View("AddNewField", newFieldList)); } var containsCurrentName = newFieldList.fields.Where(f => f.Name == newFieldList.currentName); var condatinsCreateName = newFieldList.fields.Where(f => f.Name == newFieldList.currentNameToCreate); if (condatinsCreateName.Count() != 0 || containsCurrentName.Count() != 0) { ViewBag.Error = "Pole o tej nazwie znajduje się już w formularzu!"; return(View("AddNewField", newFieldList)); } if (newFieldList.currentId != 0) { field = new FieldWithValidation { Id = newFieldList.currentId, Name = newFieldList.currentName, Type = newFieldList.currentType }; } else { field = new FieldWithValidation { Name = newFieldList.currentNameToCreate, Type = newFieldList.currentTypeToCreate }; } newFieldList.fields.Add(field); var concreteField = newFieldList.fields.Where(f => f == field).ToList()[0]; //jeśli polem jest select-> należy dodać do listy "pól do dołączenia(fields)" opcje if (newFieldList.currentTypeToCreate == "select") { List <SelectFieldOptionsJsonData> jsonDatas = JsonConvert.DeserializeObject <List <SelectFieldOptionsJsonData> >(newFieldList.optionsJson); jsonDatas = jsonDatas.Where(w => w.Wartosc != null).ToList(); foreach (var option in jsonDatas) { var selectOption = new SelectFieldOptions(); selectOption.option = option.Wartosc; concreteField.options.Add(selectOption); } } if (newFieldList.currentTypeToCreate == "number")//w przypadku gdy pole jest typu number z walidacją, oraz jest nowe(nie ma id) { decimal pom; bool parseSuccess = Decimal.TryParse(newFieldList.minString, out pom); if (parseSuccess) { newFieldList.min.value = pom; } parseSuccess = Decimal.TryParse(newFieldList.maxString, out pom); if (parseSuccess) { newFieldList.max.value = pom; } } if (newFieldList.currentTypeToCreate == "number" && field.Id == 0) { if (newFieldList.min.value != null) { newFieldList.min.type = "min"; concreteField.validations.Add(newFieldList.min); } if (newFieldList.max.value != null) { newFieldList.max.type = "max"; concreteField.validations.Add(newFieldList.max); } //kod 100 oznacza że pole może mieć tylko wartości całkowite if (newFieldList.integerVal.value == 100) { newFieldList.integerVal.type = "integerVal"; concreteField.validations.Add(newFieldList.integerVal); } } TempData.Put <NewFieldList>("newFieldListModel", newFieldList); return(RedirectToAction("ListWithFields")); }