private async Task BindModelCoreAsync(ModelBindingContext bindingContext, ViewConfigure viewConfigure) { if (bindingContext.Model == null) { bindingContext.Model = CreateModel(bindingContext); } for (var i = 0; i < bindingContext.ModelMetadata.Properties.Count; i++) { var property = bindingContext.ModelMetadata.Properties[i]; if (!CanBindProperty(bindingContext, property)) { continue; } object propertyModel = null; if (property.PropertyGetter != null && property.IsComplexType && !property.ModelType.IsArray) { propertyModel = property.PropertyGetter(bindingContext.Model); } var fieldName = property.BinderModelName ?? property.PropertyName; var modelName = ModelNames.CreatePropertyModelName(bindingContext.ModelName, fieldName); ModelBindingResult result; using (bindingContext.EnterNestedScope( modelMetadata: property, fieldName: fieldName, modelName: modelName, model: propertyModel)) { await _modelBinderProviderContext.CreateBinder(property).BindModelAsync(bindingContext); result = bindingContext.Result; } if (result.IsModelSet) { SetProperty(bindingContext, modelName, property, result, viewConfigure); } else { var descriptor = viewConfigure.GetViewPortDescriptor(modelName); if (descriptor != null && bindingContext.ModelState.ContainsKey(modelName)) { foreach (var valid in descriptor.Validator) { if (!valid.Validate(bindingContext.ModelState[modelName].RawValue)) { valid.DisplayName = descriptor.DisplayName; bindingContext.ModelState[modelName].Errors.Clear(); bindingContext.ModelState.TryAddModelError(modelName, valid.ErrorMessage); break; } } } else if (property.IsBindingRequired) { var message = property.ModelBindingMessageProvider.MissingBindRequiredValueAccessor(fieldName); bindingContext.ModelState.TryAddModelError(modelName, message); } } } bindingContext.Result = ModelBindingResult.Success(bindingContext.Model); }
public IEnumerable <T> ToList() { _viewConfigure = ServiceLocator.GetViewConfigure(EntryType); _entryProperites = EntryType.GetProperties(); SpreadsheetDocument doc = null; List <T> results = new List <T>(); try { doc = SpreadsheetDocument.Open(_excelStream, false); } catch { ErrorMessages.Add("Only support .xlsx"); } if (doc != null) { using (doc) { _workBookPart = doc.WorkbookPart; Sheet mysheet = (Sheet)doc.WorkbookPart.Workbook.Sheets.ChildElements.FirstOrDefault(); Worksheet worksheet = ((WorksheetPart)_workBookPart.GetPartById(mysheet.Id)).Worksheet; SheetData sheetData = null; foreach (var item in worksheet.ChildElements) { if (item is SheetData) { sheetData = (SheetData)item; break; } } if (sheetData != null && sheetData.ChildElements.Any()) { List <string> header = new List <string>(); var headerRow = sheetData.ChildElements.First() as Row; foreach (var cell in headerRow.ChildElements) { var cellItem = cell as Cell; header.Add(ReadCellValue(cellItem)); } for (int i = 1; i < sheetData.ChildElements.Count; i++) { T item = new T(); try { for (int j = 0; j < sheetData.ChildElements[i].ChildElements.Count; j++) { if (j < header.Count) { CellConvert(item, header[j], sheetData.ChildElements[i].ChildElements[j] as Cell); } } } catch (Exception ex) { ErrorMessages.Add(ex.Message); break; } yield return(item); } } } } }
protected virtual void SetProperty(ModelBindingContext bindingContext, string modelName, ModelMetadata propertyMetadata, ModelBindingResult result, ViewConfigure viewConfigure) { if (bindingContext == null) { throw new ArgumentNullException(nameof(bindingContext)); } if (modelName == null) { throw new ArgumentNullException(nameof(modelName)); } if (propertyMetadata == null) { throw new ArgumentNullException(nameof(propertyMetadata)); } if (!result.IsModelSet) { return; } if (propertyMetadata.IsReadOnly) { return; } var value = result.Model; try { propertyMetadata.PropertySetter(bindingContext.Model, value); var descriptor = viewConfigure.GetViewPortDescriptor(modelName); if (descriptor != null) { bool isAllValid = true; foreach (var valid in descriptor.Validator) { if (!valid.Validate(value)) { valid.DisplayName = descriptor.DisplayName; if (bindingContext.ModelState.ContainsKey(modelName)) { bindingContext.ModelState[modelName].Errors.Clear(); } bindingContext.ModelState.TryAddModelError(modelName, valid.ErrorMessage); isAllValid = false; } } if (isAllValid) { bindingContext.ModelState.MarkFieldValid(modelName); } } } catch (Exception exception) { AddModelError(exception, modelName, bindingContext); } }