public static List <EditTableField> MapObjectToEditModel <T>(T data, List <FieldAddUpdateAuto> controlLists) { List <EditTableField> editTableFields = new List <EditTableField>(); Type type = data.GetType(); foreach (FieldAddUpdateAuto control in controlLists) // each row { foreach (PropertyInfo info in type.GetProperties()) { try { if (info.Name == control.FieldName) { EditTableField editTableField = new EditTableField() { FieldName = control.FieldName, FieldValue = info.GetValue(data).ToString(), FieldType = Convert.ToInt32(control.FieldType) }; editTableFields.Add(editTableField); } } catch { } } } return(editTableFields); }
public JsonResult GetSingle(long id = 0) { object[] parms = new object[] { "@tableName", TableName }; DataTable table = new SqlFieldFilterAutoDao().GetDataTable(parms, "cofTableRenderAuto_GetAllColumnForFilter"); List <FieldAddUpdateAuto> fieldAU = new List <FieldAddUpdateAuto>(); if (table != null && table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { if (row["FieldType"] != null) { FieldAddUpdateAuto field = new FieldAddUpdateAuto() { FieldName = row["COLUMN_NAME"].ToString(), FieldType = row["FieldType"].ToString() }; fieldAU.Add(field); } } } TestQuestion TestQuestion = new SqlTestQuestionDao().GetSingle(id); EditTableField fieldId = new EditTableField() { FieldName = "ID", FieldType = 0, FieldValue = id.ToString() }; List <EditTableField> editFields = Mapper.MapObjectToEditModel <TestQuestion>(TestQuestion, fieldAU); editFields.Add(fieldId); return(Json(editFields, JsonRequestBehavior.AllowGet)); }