最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

asp.net core - Kendo UI page not binding or referencing to Model at all - Stack Overflow

matteradmin5PV0评论

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

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

Share Improve this question edited Nov 18, 2024 at 22:05 Desichele asked Nov 18, 2024 at 20:00 DesicheleDesichele 3992 gold badges6 silver badges15 bronze badges 4
  • how do you use the kendo-ui. I didn't see anythingwrong with these codes. – Qiang Fu Commented Nov 20, 2024 at 7:19
  • you mean using Kendo.Mvc & using Kendo.Mvc.UI on top of the page.. yeah I do have that still have been getting null pointer exception – Desichele Commented Nov 20, 2024 at 20:37
  • @(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? – Aleksandar Commented Nov 25, 2024 at 14:44
  • 1 Thanks for pointing to the 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. Thanks a bunch Aleksandar – Desichele Commented Nov 25, 2024 at 21:57
Add a comment  | 

1 Answer 1

Reset to default 0

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?

Post a comment

comment list (0)

  1. No comments so far