我想创建一个动态网格,它会自动动态创建列。我正在使用 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;
}
这返回到我的客户端控制器,我想将这些数据作为 Json 扔到我的 kendo ui 网格中,这里我作为字符串数据获取,但想要解析为 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);
}
请帮我一个建议。我需要使用存储过程。提前致谢,