0
votes

I have the following view that I've developed (using VS 2010 ASP.NET MVC4) as a dashboard of sorts that has four different partial views that it renders for management:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Manager.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
AMS Utility - Manager Home Page
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderID="FeaturedContent" runat="server">
<section class="featured">
    <div class="content-wrapper">
        <hgroup class="title">
            <h1>AMS Utility - Property Tax Manager's Dashboard</h1>                
        </hgroup>
    </div>
</section>  
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<% Html.RenderAction("IndexTopImports"); %>
<% Html.RenderAction("IndexTopXfrReqs"); %>
<% Html.RenderAction("IndexBURequests"); %>
<% Html.RenderAction("IndexTopCompanyRequests"); %>

</asp:Content>

Here is one of the controller actions associated with the RenderAction above (all work the same way: (1) Get the data from a DTO; (2) Return the partial view with the data):

[ChildActionOnly]
public PartialViewResult IndexTopImports()
{
    ManagersDashboardDTO importDTO = new ManagersDashboardDTO();
    return PartialView("IndexTopImports", importDTO.GetImportList());
}

Here is one of the partial views (IndexTopImports.ascx) that is called. The format for all are all the same in that I pass to the view a list of data to be displayed:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<AMSUtilityMVC4.ViewModels.MgrsDashboardActiveImportsViewModel>>" %>

<h2 style="color:Green">Most Recent State Imports</h2>

<table>
    <colgroup>
        <col span="1", style="width:25%;">
        <col span="1", style="width:35%;">
        <col span="1", style="width:35%;">
    </colgroup>

    <tr>
        <th>
            State
        </th>
        <th>
            Import Date
        </th>
        <th>
            Imported By
        </th>
    </tr>

<% foreach (var item in Model) { %>    
    <tr>
        <td>
            <%: item.stateAcronym %>
        </td>
        <td>
            <%: item.importDate %>
        </td>
        <td>
            <%: item.importedBySOEID %>
        </td>
    </tr>    
<% } %>
</table>

When I run this application locally it works just fine but when I upload it to IIS, I am getting a [NullReferenceException: Object reference not set to an instance of an object]. Here is the stack trace:

[NullReferenceException: Object reference not set to an instance of an object.] AMSUtilityMVC4.Models.MgrDashboardTopXfrRequestsDataContext..ctor() in C:\Visual Studio Projects\AMSUtilityMVC4\AMSUtilityMVC4\Models\MgrDashboardTopXfrRequests.designer.cs:38 AMSUtilityMVC4.Models.Data_Transfer_Objects.ManagersDashboardDTO..ctor() in C:\Visual Studio Projects\AMSUtilityMVC4\AMSUtilityMVC4\Models\Data Transfer Objects\ManagersDashboardDTO.cs:12 AMSUtilityMVC4.Controllers.ManagersController.IndexTopImports() in C:\Visual Studio Projects\AMSUtilityMVC4\AMSUtilityMVC4\Controllers\ManagersController.cs:73 lambda_method(Closure , ControllerBase , Object[] ) +78 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +247 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +38 System.Web.Mvc.Async.<>c__DisplayClass39.b__33() +124 System.Web.Mvc.Async.<>c__DisplayClass4f.b__49() +452 System.Web.Mvc.Async.<>c__DisplayClass37.b__36(IAsyncResult asyncResult) +15 System.Web.Mvc.Async.<>c__DisplayClass2a.b__20() +31 System.Web.Mvc.Async.<>c__DisplayClass25.b__22(IAsyncResult asyncResult) +230 System.Web.Mvc.<>c__DisplayClass1d.b__18(IAsyncResult asyncResult) +28 System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +20 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53 System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +20 System.Web.Mvc.<>c__DisplayClass8.b__3(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +20 System.Web.Mvc.<>c__DisplayClass4.b__3() +15 System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Func1 func) +41 System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +1443

[HttpException (0x80004005): Error executing child request for handler System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper.] System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +2515 System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage) +242 System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +94 System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) +693 System.Web.Mvc.Html.ChildActionExtensions.RenderAction(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues) +56 ASP.views_managers_index_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in c:\inetpub\wwwroot\AMSUtility\Views\Managers\Index.aspx:23 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +131 ASP.views_shared_manager_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\inetpub\wwwroot\AMSUtility\Views\Shared\Manager.Master:54 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +131 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +246 System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +85 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5290

Short of rewriting the view to not use partial views, I'm at a lost as to how to fix this challenge. Any ideas or suggestions?

2
Your problem has nothing to do with Partials. I suggest you read up on what a Null Reference Exception is and how to solve it. And it appears you are mixing webforms and asp.net-mvc, which I would highly recommend against.Erik Philips
I have done very, very little webforms development (almost all MVC) so in what way have I mixed webforms and MVC just so I'll know how to steer clear of it going forward?CitiDeveloper
Any tag that starts with <asp: and/or contains runat="server" are not designed with MVC (they are legacy Webforms tags). I highly recommend not using them ever, they are not needed.Erik Philips

2 Answers

0
votes

Since the NullReferenceException seems to occur inside a parameterless construtor, it is kind of hard to guess what may be going wrong. A priori, I don't see why it would be related to partial views as your question seems to indicate.

If you have access to the source code for AMSUtilityMVC4.Models.MgrDashboardTopXfrRequestsDataContext I would suggest you take a close look at the default constructor in order to see what might be causing the NullReferenceException.

If you don't have access to the source code, download a decompiler (say, Just Decompile from Telerik) and have a look at the source code through that.

0
votes

Don't run back and forth with your data layer, and from the view back to your controllers. Your view shouldn't really be doing what it's currently doing.

When your Controller Action is first executed, grab all of the data that you need, then map the data to a view model...

So what you should have is something like this...

public class ManagerDashboardViewModel
{
    public List<Import> ImportsViewModel {get;set;}
    public List<TopXfrReqs> XfrsReqsViewModel {get;set;}
    public List<BURequests> BURequestsViewModel {get;set;}
    public List<CompanyRequestsViewModel> {get;set;}
}

Then in the view you can use

@(this.Html.Partial("_partialView.cshtml", Model.ImportsViewModel))

Also, is ManagersDashboardDTO disposable? If so, where are you disposing it? If you're not disposing it, that could be causing issues like what you're experiencing too..