asp.net-corekendo-uikendo-asp.net-mvc

Kendo UI page not binding or referencing to Model at all


Must be doing something stupid that Kendo UI failed to bind to Model and I have been getting Nullpointer Exception System.NullReferenceException: Object reference not set to an instance of an object on the page

Here is the index()

public class MyController : Controller
{
 [HttpGet]
 public IActionResult Index()
 {
   return View(new MyModel { Id=123, Name="Test Name", Description="Test 
 Description"});
 }
}

And here is the Model definition:

namespace MyNamespace.Models
{
    public class MyModel
    {
        public int? Id { get; set; }
        public string? Name { get; set; }
        public string? Description { get; set; }
    }
}

and here is the razor page:

@page
@using MyNamespace.Models
@model MyModel


@if (Model != null && Model.Id != null && Model.Id > 0)
{
    <p>ID: @Model.Id</p>
    <p>Name: @Model.Name</p>
    <p>Description: @Model.Description</p>
}
else
{
    <p>Model ID is null</p>
}

ViewBag properties working fine but cant use strongly typed Model properties


Solution

  • Resolved (thanks for pointing to right direction). The culprit was the @page directive at the top of the page. This was supposed to be MVC and not a Razor page. Copied the last comment by Aleksandar below:

    @(Html.Kendo().TextAreaFor(m=>m.Description)), for example, should generate a TextArea component for the description. However there is something odd - there is a controller that is indicating a view should be loaded, but also a @page directive indicating the use of RazorPages, so which on is it - an MVC view or a RazorPage - that you are using? Does the page model define an instance of MyModel?