0

我有一个强类型局部视图,当我启动主视图时,它给了我“未设置为对象实例的对象引用”错误。我知道我没有传入任何参数,但有没有办法处理这个错误?未将对象引用设置为对象实例 - 部分视图

母版视图:

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

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Test Form 
</asp:Content> 

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

<div id="partial"> 
<% Html.RenderPartial("DisplayPartial"); %> 
</div> 

</asp:Content> 

管窥:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Student.Models.vwStudent>>" %> 

<% foreach (var item in Model) { 
      if (item == null) continue; %> 

     <tr>    
      <td> 
       <%: item.Item1%> 
      </td> 
      <td> 
       <%: item.Item2%> 
      </td> 
     </tr> 

    <% } %> 

    </table> 

回答

1

如果你需要使这个局部视图,当你没有一个模式,你当然可以测试模型并不是foreach循环

if (Model != null) 
    foreach (...) 
前空
2

你有一些模型传递到您的partialView,因为它需要的IEnumerable<Student.Models.vwStudent>

<% Html.RenderPartial("DisplayPartial", model); %> 

或者一个实例,如果模型不为null,则可以检查部分视图。

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Student.Models.vwStudent>>" %> 


<% if (Model != null) { 
    foreach (var item in Model) { 
      if (item == null) continue; %> 

     <tr>    
      <td> 
       <%: item.Item1%> 
      </td> 
      <td> 
       <%: item.Item2%> 
      </td> 
     </tr> 

    <% } 
} %> 

    </table>