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