I am using a remote web API services that I want to return an entire grid and render it on a View. I have created a <div> on the View where I want the $("#Grid").ejGrid to be rendered. The best I can get is to have it render as a HTML string but not as a control. Any ideas?
The controller method, below, is returning the string to the View successfully:
private System.Web.HtmlString CreateClientSideGrid()
{
string gridStr = string.Empty;
List<string> gridLineX = new List<string>();
gridLineX.Add("$(" + AddDoubleQuotes("#Grid") + ").ejGrid({");
gridLineX.Add("allowPaging: true,");
gridLineX.Add("columnLayout: ej.Grid.ColumnLayout.Fixed,");
gridLineX.Add("columns: [");
gridLineX.Add("{ field: " + AddDoubleQuotes("LastName") + ", headerText: " + AddDoubleQuotes("Last Name") + " },");
gridLineX.Add("{ field: " + AddDoubleQuotes("AcrNumber") + ", headerText: " + AddDoubleQuotes("Last Name") + " },");
gridLineX.Add("{ field: " + AddDoubleQuotes("DoB") + ", headerText: " + AddDoubleQuotes("Date of Birth") + " },");
gridLineX.Add("{ field: " + AddDoubleQuotes("tifURI") + ", headerText: " + AddDoubleQuotes("TIF Image") + " , template: true, templateID: " + AddDoubleQuotes("#columnTemplate") + ", width: 900, height: 450}");
gridLineX.Add("]");
gridLineX.Add("});");
for (int i = 0; i < 10; i++)
{
gridStr += gridLineX[i].ToString();
}
System.Web.HtmlString htmlString = new System.Web.HtmlString(gridStr);
return htmlString;
}
The View method, below, is run following a @Html.EJ().Button click event. All this works, its just the rendering of the grid on the View that is the problem. Note that #TestGrid is the div where I an trying to get the control to render:
function BuildGrid() {
var url = "http://localhost:40362/api/GridBuilder/1";
$.ajax({
url: url,
type: 'GET',
success: function (gridResults) {
$('#TestGrid').html(gridResults.val);
},
error: function (error) {
alert("Error: " + error);
}
});
The API Controller method is returning the following string. I paste this in the View method and it renders the grid:
$("#Grid").ejGrid({allowPaging: true,columnLayout: ej.Grid.ColumnLayout.Fixed,columns: [{ field: "LastName", headerText: "Last Name" },{ field: "AcrNumber", headerText: "Last Name" },{ field: "DoB", headerText: "Date of Birth" },{ field: "tifURI", headerText: "TIF Image" , template: true, templateID: "#columnTemplate", width: 900, height: 450}]});