Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6835119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:11:40+00:00 2026-05-26T23:11:40+00:00

I have a page containing a Chart object that I use as the default

  • 0

I have a page containing a Chart object that I use as the default route in ASP.NET MVC. When I run the app, I get an exception due to a null reference. If I use a URL with the controller and action given explicitly, everything works fine.

Here is the code to show what I mean…

In RegisterRoutes:

routes.MapRoute("Default", "{controller}/{action}", new { controller = "Dashboard", action = "Index" });

DashboardModel.cs:

public class DashboardModel
{
    public Chart MyChart { get; set; }
}

DashboardController.cs

public ActionResult Index()
{
    Chart chart = CreateChart();
    DashboardModel dm = new DashboardModel();
    dm.MyChart = chart;
    return View(dm);
}

Index.aspx

<% chartPanel.Controls.Add(Model.MyChart); %>
<asp:Panel ID="chartPanel" runat="server"></asp:Panel>

Launching the app from the debugger with the URL http://localhost:2313/ results in NullReferenceExcpetion on the first line given above in Index.aspx. If I put http://localhost:2313/Dashboard/Index in the browser, the chart is displayed correctly. I set a breakpoint in the Index() action, and it creates a valid model and chart, and the breakpoint is only hit once before the exception occurs.

Why is the model null? It should be created every time that my action method is called from what I’ve seen. There must be something more going on with the default routing that I am not understanding.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:11:40+00:00Added an answer on May 26, 2026 at 11:11 pm

    It appears that I was mistaken about the Model being null. Smashd’s question got me thinking more about the URL, and that was the cause of the exception. Although I don’t understand why the Chart needs a URL to display properly, I was able to add a workaround for the problem.

    I changed the default routing parameters as follows:

    new { controller = "Dashboard", action = "Default" }
    

    Then I added a new action method to my controller that forces a redirect:

    public ActionResult Default()
    {
        return RedirectToAction("Index");
    }
    

    This gets me to the Index with a fully formed URL. There might be a better solution for this, but at least the redirect works transparently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a normal asp.net page containing some code that I want to measure
I have an Asp.Net page containing one GridView and a couple of images (google
I have a problem with a master page containing a asp:textbox that I'm trying
I have an Asp.Net page containing one grid and an image. The image is
I have a page in a asp.net mvc shopping cart which allows you to
I have some ASP.NET MVC actions that generate images dynamically (although it could equally
The Scenario Using jQuery, I have a page that has a popup overlay containing
Having a very strange issue regarding postback in ASP.NET. I have page dynamically populating
I have an asp page containing a form with two fields where the client
I have a web page containing a form that currently print comfortably in two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.