I have a grid in a partial view and it's data source fails to call the read method on the initial page load. It does get called when I submit the ajax form, but the problem is that I want some default data to appear in the grid when the page initially loads.
Also, if I take out the model as a parameter on the read method, then the read method will in fact get called during the initial page load, but the problem with that is then I won't be able to feed it any parameters to filter the data from the ajax form.
I tried to call the read method from javascript in document.ready() to try and force it to call the read method, but that didn't work. Is there a way I can make the read method get called in the initial page load while also feeding it the model as a parameter?
Here is the partial view called _ProjectResults
@model MvcTestProject.Models.ProjectSearchModel
@(Html.Kendo().Grid<
MvcTestProject.Models.ProjectItem
>()
.Name("projectresultsgrid")
.Columns(columns =>
{
columns.Bound(p => p.KeyID).Title("Key ID").Width(130);
columns.Bound(p => p.StoreNumber).Title("Store").Width(130);
columns.Bound(p => p.ProjectType).Title("Type").Width(150);
columns.Bound(p => p.ProjectStatus).Title("Status").Width(130);
columns.Bound(p => p.InstallStartDate).Title("Start Date").Width(130).Format("{0:MM/dd/yyyy}");
})
.Pageable()
.Sortable()
.Scrollable(scr => scr.Height(550))
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(50)
.ServerOperation(false)
.Read(data => data.Action("ReadProjects", "Project", Model))
)
)
Here is the parent page
@model MvcTestProject.Models.ProjectSearchModel
@{
Layout = "~/Views/Shared/_Layout_Project.cshtml";
ViewBag.Title = "Index";
}
<
h2
>Project Search</
h2
>
<
div
>
@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "dvProjectResults" }))
{
<
fieldset
style
=
"margin:10px;"
>
<
legend
style
=
"margin:5px;font-weight:bold"
>
Search Criteria
</
legend
>
<
table
>
<
tr
>
<
td
>@Html.LabelFor(m => Model.ProjectTypeID)</
td
>
<
td
width
=
"400px;"
>
@(Html.Kendo().DropDownListFor(m => m.ProjectTypeID)
.HtmlAttributes(new { style = "width: 250px" })
.DataTextField("TypeName")
.DataValueField("ProjTypeID")
.BindTo(Model.ProjectTypeOptions)
.OptionLabel(" ")
)
</
td
>
</
tr
>
<
tr
>
<
td
>@Html.LabelFor(m => Model.ProjectStatusID)</
td
>
<
td
>
@(Html.Kendo().DropDownListFor(m => m.ProjectStatusID)
.HtmlAttributes(new { style = "width: 250px" })
.DataTextField("StatusName")
.DataValueField("ProjStatusID")
.BindTo(Model.ProjectStatusOptions)
.OptionLabel(" ")
)
</
td
>
<
td
>@Html.LabelFor(m => Model.StartDate)</
td
>
<
td
>@Html.EditorFor(m => Model.StartDate)</
td
>
</
tr
>
<
tr
>
<
td
>@Html.LabelFor(m => Model.KeyID)</
td
>
<
td
>@Html.EditorFor(m => Model.KeyID)</
td
>
<
td
>@Html.LabelFor(m => Model.EndDate)</
td
>
<
td
>@Html.EditorFor(m => Model.EndDate)</
td
>
</
tr
>
<
tr
>
<
td
>@Html.LabelFor(m => Model.StoreNumber)</
td
>
<
td
>@Html.EditorFor(m => Model.StoreNumber)</
td
>
</
tr
>
<
tr
>
<
td
colspan
=
"2"
><
input
type
=
"submit"
value
=
"Search"
/></
td
>
</
tr
>
</
table
>
</
fieldset
>
}
<
div
id
=
"dvProjectResults"
style
=
"margin:10px;"
>
@{Html.RenderPartial("_ProjectResults", Model);}
</
div
>
</
div
>
<
script
type
=
"text/javascript"
>
$(document).ready(function () { JSReadProjects(); });
function JSReadProjects() {
var grid = $("#projectresultsgrid").data("kendoGrid");
grid.dataSource.read();
}
</
script
>
Here is the http post actionresult that is executed when the ajax form is submitted:
[HttpPost]
public
ActionResult ProjectSearch(ProjectSearchModel model)
{
return
PartialView(
"_ProjectResults"
, model);
}
Here is the read method on the controller:
public
ActionResult ReadProjects([DataSourceRequest] DataSourceRequest request, ProjectSearchModel searchModel)
{
var data = datamgr.GetProjects
(
searchModel.KeyID,
searchModel.StoreNumber,
searchModel.ProjectTypeID,
searchModel.ProjectStatusID,
searchModel.StartDate,
);
var serializer =
new
JavaScriptSerializer();
var result =
new
ContentResult();
serializer.MaxJsonLength = Int32.MaxValue;
// Whatever max length you want here
result.Content = serializer.Serialize(data.ToDataSourceResult(request));
result.ContentType =
"application/json"
;
return
result;
}