System.NullReferenceException: 'Object reference not set to an instance of an object.' when trying to run new View.

Mel 21 Reputation points
2022-09-23T04:42:24.867+00:00

Can someone please have a look at my code and help me understand why I'm getting this error? I have set up a controller and model, but when trying to run this view I get this error.244080-2022-09-23.png

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,237 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,231 Reputation points Microsoft Vendor
    2022-09-23T06:16:02.24+00:00

    Hi @Mel ,

    System.Web.Mvc.WebViewPage<TModel>.Model.get returned null

    The error is because you haven't defined Model in your View.
    You need to fill your model and send it to the View.
    Try the following:
    .cshtml

    @model MVCname.Models.Your Model   
    

    Controller

    public ActionResult Index()  
    {  
        using (MvcDB dataBase = new MvcDB())  
        {  
      
        }  
        return View(< YourModel > );  
    }  
    

    If possible, you can provide code that reproduces the problem so we can better help you troubleshoot.

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful