I'm trying to apply a "filter" in a kendo UI grid in which if a property of the ViewModel is true then show the value of cell with a link address, if false then just show the value without an address.The viewModel for this grid has a property called IsRecorded which it is used as filter for the previously explained.
But for some reason I keep getting the following error just when I run the project and enter to the module where the Kendo Grid is:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Web.Mvc.WebViewPage.Model.get returned null.
And this is my kendo .cshtml
@(Html.Kendo().Grid<PersonReportViewModel>()
.Name("Report")
.Columns(columns =>
{
columns.Bound(m => m.Name)
.HeaderHtmlAttributes(new { @class = "Name" }).HtmlAttributes(new { @class = "Name" });
if (Model.IsRecorded)
{
columns.Bound(m => m.Id)
.HeaderHtmlAttributes(new { @class = "Id" }).HtmlAttributes(new { @class = "Id" })
.ClientTemplate("<a href=\"SomeAdress"</a>");
}
else
{
columns.Bound(m => m.Id)
.HeaderHtmlAttributes(new { @class = "Id" }).HtmlAttributes(new { @class = "Id" });
};
Am I accessing it wrong with the Model class in the if condition? The weird part is that when using Model. it shows me all the properties that the viewmodel has, like it is accessing it correctly but its returning null. Is the condition implementation right also at all?
Thanks for your Answers.
Model.IsRecorded would look at the page model, not the grid model (PersonReportViewModel). A couple of ways to achieve that are shown here. I would use a ClientTemplate:
columns.Template(t => {}).ClientTemplate(
"# if (IsRecorded) {#"
+ "<a href=\'SomeAdress'</a>"
+ "# } else { #"
+ @"<span>#=JobId#</span>"
+ "# } #")
.HeaderHtmlAttributes(new { @class = "Id" })
.HtmlAttributes(new { @class = "Id" })
.Title("Job Id");
A console error will occur if you have errors like missing #
or {
.