对象引用不设置到对象的实例 - 管窥 [英] Object reference not set to an instance of an object - Partial View
本文介绍了对象引用不设置到对象的实例 - 管窥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我这是给我的错误,当我启动母版视图不设置到对象的实例对象引用一个强类型的局部视图。我知道我没有传递任何参数还没有,但有一种方法来处理这个问题?
母版视图:
<%@页标题=LANGUAGE =C#的MasterPageFile =〜/查看/共享/的Site.Master继承=System.Web.Mvc.ViewPage<动态>中%GT;< ASP:内容ID =内容1ContentPlaceHolderID =TitleContent=服务器>
测试表单
< / ASP:内容>< ASP:内容ID =内容2ContentPlaceHolderID =日程地址搜索Maincontent=服务器>< DIV ID =部分>
&所述;%Html.RenderPartial(DisplayPartial); %GT;
< / DIV>< / ASP:内容>
局部视图:
<%@控制语言=C#继承=System.Web.Mvc.ViewUserControl<&IEnumerable的LT; Student.Models.vwStudent>>中%GT;<%的foreach(在型号VAR项){
如果(项目== NULL)继续; %GT; &所述; TR>
&所述; TD>
<%:item.Item1%GT;
< / TD>
&所述; TD>
<%:item.Item2%GT;
< / TD>
< / TR> <%}%GT; < /表>
解决方案
如果你需要使这个局部视图,当你没有一个模式,你当然可以测试模型并不是foreach循环之前,空
如果(型号!= NULL)
的foreach(...)
I have a strongly typed partial view which is giving me "Object reference not set to an instance of an object" error when I launch the master view. I know I am not passing in any parameters yet, but is there a way to handle this error?
Master View:
<%@ 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>
Partial View:
<%@ 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>
解决方案
If you need to render this partial view when you don't have a Model, you can certainly test that Model is not null before the foreach loop
if (Model != null)
foreach (...)
这篇关于对象引用不设置到对象的实例 - 管窥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文