Object reference not set to an instance of an object - Partial View

前端 未结 2 1850
傲寒
傲寒 2020-12-22 09:05

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

相关标签:
2条回答
  • 2020-12-22 09:42

    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 (...)
    
    0 讨论(0)
  • 2020-12-22 09:43

    You have to pass some Model to your partialView, because it need a instance of IEnumerable<Student.Models.vwStudent>

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

    Or, you can check in your partial view if the model is not 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>
    
    0 讨论(0)
提交回复
热议问题