Question

I've got this code in an MVC project using the WebForms view engine and I'm trying to convert it over to Spark. How can I conditionally call a partial and pass it view data?

<% if (UserService.IsAuthenticated && !Model.Post.IsDeleted) { %>
    <% Html.RenderPartial("Reply", new ReplyViewModel { Id=Model.Post.PostId, CssClass="respond" }); %>
<% } %>

Tried this (to no avail, it renders the partial before all other content):

<if condition="UserService.IsAuthenticated && !Model.Post.IsDeleted">
    #Html.RenderPartial("Reply", new ReplyViewModel { Id=Model.Post.PostId, CssClass="respond" });
</if>
Was it helpful?

Solution

The

<% if (UserService.IsAuthenticated && !Model.Post.IsDeleted) { %>
    <% Html.RenderPartial("Reply", new ReplyViewModel { Id=Model.Post.PostId, CssClass="respond" }); %>
<% } %>

and

<if condition="UserService.IsAuthenticated && !Model.Post.IsDeleted">
    #Html.RenderPartial("Reply", new ReplyViewModel { Id=Model.Post.PostId, CssClass="respond" });
</if>

and the <test if=""> variation should all work and produce nearly identical code:

if (UserService.IsAuthenticated && !Model.Post.IsDeleted) 
{ 
    Html.RenderPartial("Reply", new ReplyViewModel { Id=Model.Post.PostId, CssClass="respond" });
}

Maybe try outputting ${UserService.IsAuthenticated} and ${Model.Post.IsDeleted} to be absolutely certain the condition isn't always true?


Okay - confirmed in another medium that's incorrect... Is it possible the "Reply" partial is a WebForms view like Reply.ascx or Reply.aspx? There is an issue with WebForms in that it's output by default will go directly to the current HttpContext response output, which makes it difficult to interleave those partials with view engines that spool or layer output.

There's a way to work around that in one of the Spark samples, but it's a bit tricky.

OTHER TIPS

Try with the test if="" syntax

<test if="UserService.IsAuthenticated && !Model.Post.IsDeleted">
    ${Html.RenderPartial("Reply", new ReplyViewModel { Id=Model.Post.PostId, CssClass="respond" });}
</test>

Thanks to Louis' help on Twitter, the problem was that the partial being called was an .ascx file and not a .spark file. I had not yet deleted the old, unconverted .ascx file. Once the Reply.ascx was deleted, everything worked as expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top