首页 文章

表单上的多个PartialViews产生NullReferenceException:对象引用未设置为对象的实例

提问于
浏览
0

我有以下观点,我已经开发了(使用VS 2010 ASP.NET MVC4)作为各种各样的仪表板,它有四个不同的部分视图,它们呈现给管理:

<%@ 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>

以下是与上述RenderAction相关的控制器操作之一(所有操作方式相同:(1)从DTO获取数据;(2)返回包含数据的局部视图):

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

这是被调用的部分视图(IndexTopImports.ascx)之一 . 所有格式都是相同的,因为我向视图传递了要显示的数据列表:

<%@ 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>

当我在本地运行这个应用程序它工作得很好,但当我上传到IIS时,我得到一个[NullReferenceException:对象引用未设置为对象的实例] . 这是堆栈跟踪:

[NullReferenceException:对象引用未设置为对象的实例 . ] C:\ Visual Studio Projects \ AMSUtilityMVC4 \ AMSUtilityMVC4 \ Models \ MgrDashboardTopXfrRequests.designer.cs中的AMSUtilityMVC4.Models.MgrDashboardTopXfrRequestsDataContext..ctor():38 AMSUtilityMVC4.Models C:\ Visual Studio Projects \ AMSUtilityMVC4 \ AMSUtilityMVC4 \ Models \ Data Transfer Objects \ ManagersDashboardDTO.cs中的.Data_Transfer_Objects.ManagersDashboardDTO..ctor():C:\ Visual Studio Projects \ AMSUtilityMVC4中的12 AMSUtilityMVC4.Controllers.ManagersController.IndexTopImports() \ 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参数)38 System.Web.Mvc.Async . <> c__DisplayClass39.b__33()124 System.We b.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系统 . 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系统 . 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处理程序,TextWriter编写器,Boolean preserveForm,Boolean setPreviousPage,VirtualPath路径,VirtualPath filePath,字符串physPath,异常错误,字符串queryString覆盖)1443 [HttpException(0x80004005):执行处理程序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_in dex_aspx .__ RenderContent2(HtmlTextWriter __w,Control parameterContainer)位于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)位于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)246System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer)85 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)5290

如果不重写视图以不使用部分视图,我就不知道如何解决这个挑战 . 任何想法或建议?

2 回答

  • 0

    由于NullReferenceException似乎发生在无参数的construtor中,因此很难猜出可能出现的问题 . 先验,我不明白为什么它会与部分观点有关,因为你的问题似乎表明了这一点 .

    如果您有权访问_2922149的源代码,我建议您仔细查看默认构造函数,以查看可能导致NullReferenceException的原因 .

    如果您无权访问源代码,请下载反编译器(例如,Just Decompile from Telerik)并查看源代码 .

  • 0

    不要与数据层来回运行,也不要从视图回到控制器 . 你的观点不应该真正做到它目前正在做的事情 .

    首次执行Controller Action时,获取所需的所有数据,然后将数据映射到视图模型......

    所以你应该拥有的是这样的......

    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;}
    }
    

    然后在视图中你可以使用

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

    还有, ManagersDashboardDTO 一次性?如果是这样,你在哪里处理它?如果你're not disposing it, that could be causing issues like what you'再次体验..

相关问题