public async Task <IActionResult> Detail(UrlParameter param) { var model = new ViewListModel(); model.Table = (await _tableListService.GetByIdAsync(param.extraValue.Ext_ToInt32())).data; var result = await _tableColumnService.GetListAsync(item => item.TableId == model.Table.Id && item.ViewVisible == 1); model.TableColumn = result.data.OrderBy(c => c.ViewOrder).ToList(); string sql = "select {0} from {1} where {2}"; ViewBag.OutColumn = new Dictionary <string, object>(); var tbName = model.Table.Name; var columnValueResult = await SqlService.Query( string.Format(sql, "*", tbName, "Id=" + param.id)); var columnValue = columnValueResult.First(); ViewBag.ColumnValue = columnValue; foreach (var column in model.TableColumn) { if (column.DataType == ColumnType.Out) { var outSqlModel = new OutSqlModel(column.OutSql); var outColValue = await SysService.GetOutValue(outSqlModel, columnValue[column.Name].ToString()); ViewBag.OutColumn[column.Name] = outColValue.data; } else if (column.DataType == ColumnType.MultiSelect_Out) { var outSqlModel = new OutSqlModel(column.OutSql); var queryResult = await SysService.GetOutData(outSqlModel); //如果保存到外表 查询的是外表值 if (outSqlModel.IsSave) { var selectValues = (await SysService.GetMultiSelectOutValue(outSqlModel, param.id.ToString())).data; var outColValues = queryResult.Where(item => selectValues.Contains(item[outSqlModel.PrimaryKey].ToString())) .Select(item => item[outSqlModel.TextKey]).ToList(); ViewBag.OutColumn[column.Name] = string.Join(",", outColValues); } //否则查询本表此列值 else { ViewBag.OutColumn[column.Name] = columnValue[column.Name]; } } else if (column.DataType == ColumnType.MultiSelect) { var selectValues = columnValueResult.First()[column.Name].ToString().Split(','); var checkStr = column.SelectRange.Split('|'); //1,选项1|2,选项2 var items = new List <SelectListItem>(); var selectText = string.Empty; foreach (var item in checkStr) { if (selectValues.Contains(item.Split(',')[0])) { selectText += item.Split(',')[1] + ","; } } ViewBag.ColumnValue[column.Name] = selectText.Trim(','); } } return(View(model)); }
public async Task <IActionResult> Edit(UrlParameter param) { var model = new ViewListModel(); model.Table = (await _tableListService.GetByIdAsync(param.extraValue.Ext_ToInt32())).data; var result = await _tableColumnService.GetListAsync(item => item.TableId == model.Table.Id && item.EditVisible == 1); model.TableColumn = result.data.OrderBy(c => c.EditOrder).ToList(); var tbName = model.Table.Name; var columnValue = (await SqlService.Query( string.Format("select {0} from {1} where {2}", "*", tbName, "Id=" + param.id))).First(); ViewBag.ColumnValue = columnValue; var fileData = new Dictionary <string, IList <object> >(); ViewData["FileData"] = ""; foreach (var column in model.TableColumn) { var colValue = string.Empty; if (columnValue.ContainsKey(column.Name)) { colValue = columnValue[column.Name].ToString(); } if (column.DataType == ColumnType.Out) { var outSqlModel = new OutSqlModel(column.OutSql); var queryResult = await SysService.GetOutData(outSqlModel); var items = new List <SelectListItem>(); items.Add(new SelectListItem { Value = "0", Text = "无", }); //遍历outsql查询的数据中全部项 foreach (var row in queryResult) { items.Add(new SelectListItem { Value = row[outSqlModel.PrimaryKey].ToString(), Text = row[outSqlModel.TextKey].ToString(), }); } ViewData[column.Name] = new SelectList(items, "Value", "Text", colValue); } else if (column.DataType == ColumnType.MultiSelect_Out) { var outSqlModel = new OutSqlModel(column.OutSql); var queryResult = await SysService.GetOutData(outSqlModel); var items = new List <SelectListItem>(); //遍历outsql查询的数据中全部项 foreach (var row in queryResult) { items.Add(new SelectListItem { Value = row[outSqlModel.PrimaryKey].ToString(), Text = row[outSqlModel.TextKey].ToString(), }); } var selectValues = new List <object>(); if (outSqlModel.IsSave) { selectValues = (await SysService.GetMultiSelectOutValue(outSqlModel, param.id.ToString())).data.ToList(); } else { foreach (var item in colValue.Split(',')) { selectValues.Add(item); } } ViewData[column.Name] = new MultiSelectList(items, "Value", "Text", selectValues); } else if (column.DataType == ColumnType.Enum) { //获取此条数据列类型为Enum的字段的值,以便之后SelectList的默认选中Selected使用 var enumStr = column.EnumRange.Split(','); //value,value var items = new List <SelectListItem>(); for (int i = 0; i < enumStr.Length; i++) { items.Add(new SelectListItem { Value = enumStr[i], Text = enumStr[i], }); } ViewData[column.Name] = new SelectList(items, "Value", "Text", colValue); } else if (column.DataType == ColumnType.MultiSelect) { var selectValues = colValue.Split(','); var checkStr = column.SelectRange.Split('|'); //1,选项1|2,选项2 var items = new List <SelectListItem>(); foreach (var item in checkStr) { items.Add(new SelectListItem { Selected = selectValues.Contains(item.Split(',')[0]), Value = item.Split(',')[0], Text = item.Split(',')[1], }); } ViewData[column.Name] = new MultiSelectList(items, "Value", "Text", selectValues); } else if (column.DataType == ColumnType.File || column.DataType == ColumnType.Image) { var files = colValue.Split(','); fileData.Add(column.DataType + column.Name, new List <object>()); foreach (var file in files) { fileData[column.DataType + column.Name].Add(new { fileUrl = file, fileType = Path.GetExtension(file).TrimStart('.'), fileName = Path.GetFileNameWithoutExtension(file), columnType = column.DataType }); } } } if (fileData.Keys.Count > 0) { ViewData["FileData"] = JsonConvert.SerializeObject(fileData); } return(View(model)); }