0

I want to create a dynamic grid which auto creates the column dynamically.Iam using WebAPi to return the result

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;
        }

Here I am getting the entire grid data which has to be displayed dynamically from stored procedure.

This is like a proxy layer where i get the data from the service like this.

        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;
        }

This returned to my client controller where i want to throw this data to my kendo ui grid as Json,here iam getting as string data but want to parse to json without serializing it.

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 help me with a suggestion.I required to use the stored procedure. Thanks in advance,

  • using json it sounds like you want to add columns on the fly from the view. Remember that once a view is generated all of the helpers are gone. You just have html at that point. You will need to use an ajax call and change the table using jquery like here http://stackoverflow.com/questions/1075415/jquery-add-html-table-column – Matt Bodily Nov 08 '13 at 13:54

1 Answers1

0

Do you use the MVC wrappers? If you are using the web (JS) version then you can try to use the schema.parse function to intercept and parse that long string into JSON array.

Petur Subev
  • 19,983
  • 3
  • 52
  • 68