对象引用未设置为对象渲染局部视图的实例

时间:2017-01-06 05:23:54

标签: asp.net-mvc rendering partial-views

以下是我的部分视图电话:

 <div id="div-reviewgrid">

                        @Html.Partial("_ReviewList", ViewBag.RewiewLists as List<Pollit.Data.Review>)

                    </div>

这是我在Partial:

下的共享文件夹中的部分视图
@using Pollit.Data;

@{
    List<Review> reviewList = ViewBag.RewiewLists;
}

<br /><br />

@if (reviewList != null)
{
    foreach (var review in reviewList)
    {
        <div class="col-lg-12">
            <div id="@review.Id">
                <div class="col-lg-1 col-md-1 col-xs-12 col-sm-12">
                    @if (review.Rating != null)
                    {
                        <img src="/images/ratings/@review.Rating.Number-bars.png" class="pull-left" style="max-height: 50px; max-width: 50px; margin-left: 30px" />
                    }
                </div>
                <div class="col-lg-11 col-md-11 col-xs-12 col-sm-12">
                    <span>@review.Creator.Name &nbsp;@String.Format("{0:d}", review.Created)</span>
                    <br />

                    <h4 class="custum-memphisfontmediumitalic ">@review.Content</h4>
                    @*<input type="submit" value="Like" class="btn btn-info" />
                        <input type="submit" id="click" value="Comment" class="btn btn-info"  />
                         <input type="text" id="comment" placeholder="Enter your comment" class="form-control" />*@
                </div>


            </div>
            <br />
            @if (Request.IsAuthenticated == true)
            {

                if (review.Replys != null)
                {
                    foreach (var reply in review.Replys.OrderBy(c => c.Created))
                    {
                        <div id="reply_@reply.ReplyID" class="col-lg-8 col-md-10 col-xs-12 col-sm-12 col-lg-offset-1 ">



                            <span>Reply By:@reply.Creator.Name &nbsp;@String.Format("{0:d}",  @reply.Created)</span>
                            <br />
                            <h4 class="custum-memphisfontmediumitalic">@reply.ReplyContent</h4>
                        </div>
                    }
                }

                <div class="col-lg-8 col-md-10 col-xs-12 col-sm-12 col-lg-offset-1 form-group ">
                    <input type="text" id="txtReply_@review.Id" placeholder="Enter your reply" class="form-control" />
                </div>
                <div class="col-lg-2 form-group"><div class="demo">
                 <a href= "SubmitReply(@review.Id); return false;" onclick="SubmitReply(@review.Id); return false;" class="pull-right">  Reply</a>
                    <img src="/Images/Comment rate up.png" class="img-responsive" width="40px" ><img src="/Images/Comment rate down.png" class="img-responsive" width="40px" ></div>
                </div>
            }
        </div>
    }
}




<br /><br />

以上是我的部分视图代码,所以大家告诉我,如果我做错了什么。请帮我。在此先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

当你以错误的方式调用局部视图时。每当你调用它时,新模型就会生成,你的模型就会变空了

@Html.Partial("_ReviewList", ViewBag.RewiewLists as List<Pollit.Data.Review>)

Model而不是ViewBag.RewiewLists as List<Pollit.Data.Review>