public JsonResult GetDefaultChoices(string dbTable, string param, bool autoBind = true) { var choices = new List <BaseEamListItem>(); if (!autoBind && string.IsNullOrEmpty(param)) { return(Json(choices)); } string query = string.Format(SqlTemplate.GetAll(), "Id", "Name", dbTable, param); IEnumerable <dynamic> result = null; result = _dapperContext.Query(query); foreach (IDictionary <string, object> obj in result) { choices.Add(new BaseEamListItem { Id = obj["Id"].ToString(), Name = obj["Name"].ToString() }); } //add empty value if (choices.Count > 0) { choices.Insert(0, new BaseEamListItem { Id = "", Name = "" }); } return(Json(choices)); }
public JsonResult GetChoices(string dbTable, string dbTextColumn, string dbValueColumn, string param, bool autoBind = true) { var choices = new List <SelectListItem>(); if (!autoBind && string.IsNullOrEmpty(param)) { return(Json(choices)); } string query = string.Format(SqlTemplate.GetAll(), dbValueColumn, dbTextColumn, dbTable, param); IEnumerable <dynamic> result = null; result = _dapperContext.Query(query); foreach (IDictionary <string, object> obj in result) { choices.Add(new SelectListItem { Value = obj[dbValueColumn].ToString(), Text = obj[dbTextColumn].ToString() }); } //add empty value if (choices.Count > 0) { choices.Insert(0, new SelectListItem { Value = "", Text = "" }); } return(Json(choices.OrderBy(c => c.Text))); }