我想创建一个动态网格,动态自动创建列。我使用WebAPi 返回结果
public dynamic SqlDataReaderToExpando(System.Data.Common.DbDataReader reader)
{
var expandoObject = new ExpandoObject() as IDictionary<string, object>;
for (var i = 0; i < reader.FieldCount; i++)
expandoObject.Add(reader.GetName(i), reader[i]);
return expandoObject;
}
public IEnumerable<dynamic> GetDynamicSqlData()
{
using (var cps = new CPS.Entities.CPSContext())
{
using (var cmd = cps.Database.Connection.CreateCommand())
{
cps.Database.Connection.Open();
cmd.CommandText = "dbo.SP_CPSPortalModuleRoles";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
System.Data.SqlClient.SqlParameter param = new System.Data.SqlClient.SqlParameter();
param.Value = Guid.Empty;
param.ParameterName = "ModuleId";
cmd.Parameters.Add(param);
using (var reader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)) //System.Data.CommandBehavior.SequentialAccess
{
while (reader.Read())
{
yield return SqlDataReaderToExpando(reader);
}
}
}
}
}
public HttpResponseMessage GetRoleDetail()
{
HttpResponseMessage response = new HttpResponseMessage();
try
{
RoleItem collSelect = new RoleItem();
IEnumerable<dynamic> collItem = null;
collItem = GetDynamicSqlData().ToList();
collSelect.Item = collItem;
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.RegisterConverters(new JavaScriptConverter[] { new ExpandoJsonConverter() });
string jsonOfTest = javaScriptSerializer.Serialize(collSelect);
response.Content = new ObjectContent<string>(jsonOfTest, new JsonMediaTypeFormatter(), Mime.AppJson);
response.StatusCode = HttpStatusCode.OK;
}
catch (Exception ex)
{
response.StatusCode = HttpStatusCode.ExpectationFailed;
response.Headers.Add(Verb.ExceptionMessage, ex.Message.ToResponseString());
response.Headers.Add(Verb.ExceptionStackTrace, ex.StackTrace.ToResponseString());
}
return response;
}
这里我得到的整个网格数据必须从存储过程中动态显示。
这就像一个代理层,我从服务中获取数据。
public String GetRoleMappings()
{
RoleItem collection = null;
string data1 = "";
try
{
using (HttpClient httpClient = new HttpClient())
{
Task<HttpResponseMessage> tskResponse = (httpClient.GetAsync(uri + @"/GetRoleDetail/"));
if (!tskResponse.IsFaulted)
{
HttpResponseMessage response = tskResponse.Result;
response.EnsureSuccessStatusCode();
string data = response.Content.ReadAsStringAsync().Result;
data1 = JsonConvert.DeserializeObject<string>(data, ServiceUtility.JsonSettings);
string data2 = JsonConvert.DeserializeObject<string>(data1, ServiceUtility.JsonSettings);
}
}
}
catch (Exception ex)
{
logger.LogError(ex);
}
return data1;
}
这返回到我的客户端控制器,我想把这些数据丢给我的kendo ui网格 作为Json,我在这里获取字符串数据但想要解析为json而不进行序列化。
public ActionResult GetRoleMapping([DataSourceRequest] DataSourceRequest request)
{
JsonResult jsonResult = null;
string s = objService.GetRoleMappings();
//JsonResult jsonResult = null;
//jsonResult = Json(objModel.Item.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
//var subItems = _subItems.Where(o => o.ItemId == itemId);
//DataSourceResult result = objModel.ToDataSourceResult(request);
//return s;
return Content(s);
//return new stringContent( (s, "application/json");
//return Json(jsonResult);
}
Plz帮我提一个建议。我需要使用存储过程。 提前谢谢,