This is a migrated thread and some comments may be shown as answers.

Kendo Grid receiving any object

1 Answer 136 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 26 Mar 2015, 06:16 PM
Hello Telerik Team,

I want my kendo grid built in Asp.Net MVC 4 receive any object model, in other words, dynamic. The action method of my controller not show errors, but the data not displayed in kendo grid. Any suggestions?

View:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
 <% Html.Kendo().Grid<dynamic>()
    .Name("GridTeste")
    .Columns(columns =>
    {
        if (Model != null)
        {
            foreach (var x in Model)
            {
                 columns.Bound(x.Value, x.Key.ToString()).Title(x.Key).Encoded(false);
            }
        }
    }
    )
         .DataSource(datasource => datasource
         .Ajax()
         .Read(read => read.Action("CarregarDados", "ImportacaoTabelaPropria", new { acao = "Brasindice", ajuste = "", nova_validade = "" }))
         )
        .Navigatable()
        .Scrollable()
        .Selectable()
        .HtmlAttributes(new { style = "width:100%;height:400px;", id = "GridTeste" })
        .Render();
 %>

My Action in Controller: 

        public ActionResult CarregarDados(string acao, string ajuste, string nova_validade, [DataSourceRequest] DataSourceRequest request)
        {
            PreencherTabelaPropria tabela_propria = new PreencherTabelaPropria();
            PreencherBrasindice bras = new PreencherBrasindice();
            Dictionary<string,System.Type> colunas = new Dictionary<string,Type>();
            if (acao.Equals("Tabela propria"))
            {
                return Json(tabela_propria.PreencherTabelaPropriaValores(), JsonRequestBehavior.AllowGet);
            }
            if (acao.Equals("Brasindice"))
            {
                if (bancoBrasindice != null)
                {
                    if (!String.IsNullOrEmpty(ajuste))
                    {
                        foreach (var x in bancoBrasindice)
                        {
                            x.Ajuste_liberado = ajuste;
                            if (!String.IsNullOrEmpty(nova_validade))
                                x.Nova_validade = DateTime.Parse(nova_validade);
                        }
                        return Json(bancoBrasindice, JsonRequestBehavior.AllowGet);
                    }
                }
                else
                {
                    List<ImportacaoBrasindiceModel> brasindice = bras.PreencherBrasindiceValores();
                    //atualizar o modelo com os valores de ajuste e validade
                    foreach (var x in brasindice)
                    {
                        x.Ajuste_liberado = ajuste;
                        if (!String.IsNullOrEmpty(nova_validade))
                            x.Nova_validade = DateTime.Parse(nova_validade);
                    }
                    bancoBrasindice = brasindice;
                }
                Dictionary<string, System.Type> colunas2;
                PropertyInfo[] propriedades = typeof(ImportacaoBrasindiceModel).GetProperties();
                colunas2 = new Dictionary<string, Type>();
                foreach (PropertyInfo x in propriedades)
                {
                    colunas.Add(x.Name, x.PropertyType);
                }
                //return PartialView("~/Views/ImportacaoTabelaPropria/TesteGridParcial.ascx", Json(bancoBrasindice, JsonRequestBehavior.AllowGet));
                return Json(bancoBrasindice.ToDataSourceResult(request), RenderPartialViewToString("~/Views/ImportacaoTabelaPropria/TesteGridParcial.ascx", colunas), JsonRequestBehavior.AllowGet);

            }
            return Content("");
        }

My method to convert Partial View to string: 

protected string RenderPartialViewToString(string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = ControllerContext.RouteData.GetRequiredString("action");

            ViewData.Model = model;

            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);

                return sw.GetStringBuilder().ToString();
            }
        }


















1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 30 Mar 2015, 04:29 PM
Hello Eric,

Could you provide a sample response from the server? From the code it is not clear what exactly is returned from the read action. Also, note that the grid will not automatically recreate the columns based on the response from the read action. You should either reload the grid from a partial view via Ajax or use the setOptions method to set the new columns if the columns should be changed.

Regards,
Daniel
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
Grid
Asked by
Eric
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or