0

I'm getting this exception while trying to render PartialView. When I change @{Html.RenderPartial("_ChildReplies", parRep.ChildReplies);}
to @Html.Partial("_ChildReplies", parRep.ChildReplies) still getting same exception.

@model List<Reply>

@using YourPlace.Models

<ul>
@foreach (var parRep in Model)
{
    <li>
        Author: @parRep.AuthorName
        Comment: @parRep.AuthorName
        <div>
        @{Html.RenderPartial("_ChildReplies", parRep.ChildReplies);}
        </div>
    </li>
}
</ul>
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
bobeq
  • 9
  • 4

1 Answers1

0

The code above looks correct... if you are getting this error, it is probably because something is going wrong inside _ChildReplies partial view which you have not shown in your question...


Good explanation here:

  • RenderPartial() is a void method that writes to the response stream. A void method, in C#, needs a ; and hence must be enclosed by { }.
  • Partial() is a method that returns an MvcHtmlString. In Razor, You can call a property or a method that returns such a string with just a @ prefix to distinguish it from plain HTML you have on the page.

So you need to either use this:

@{ Html.RenderPartial("_ChildReplies", parRep.ChildReplies); }

Or this:

@Html.Partial("_ChildReplies", parRep.ChildReplies);
Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
  • Thanks. I checked _ChildReplies PartialView and indeed there was a source of error. I wrapped '@{ Html.RenderPartial("_ChildReplies", parRep.ChildReplies); }' in 'if block' so simply removing '@' fixed the problem. – bobeq Dec 29 '18 at 12:11