Spark 中此 MVC 视图代码的等效语法是什么?
我在使用 WebForms 视图引擎的 MVC 项目中获得了此代码,我正在尝试将其转换为 Spark。 如何有条件地调用部分并传递给它查看数据?
<% if (UserService.IsAuthenticated && !Model.Post.IsDeleted) { %>
<% Html.RenderPartial("Reply", new ReplyViewModel { Id=Model.Post.PostId, CssClass="respond" }); %>
<% } %>
尝试过此操作(无济于事,它在所有其他内容之前呈现部分内容):
<if condition="UserService.IsAuthenticated && !Model.Post.IsDeleted">
#Html.RenderPartial("Reply", new ReplyViewModel { Id=Model.Post.PostId, CssClass="respond" });
</if>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
and
和
也许尝试输出 ${UserService.IsAuthenticated} 和 ${Model.Post.IsDeleted} 以绝对确定条件并不总是正确?
好的 - 在另一种媒体中确认这是不正确的...“回复”部分是否可能是像 Reply.ascx 或 Reply.aspx 这样的 WebForms 视图? WebForms 存在一个问题,默认情况下它的输出将直接转到当前 HttpContext 响应输出,这使得很难将这些部分与假脱机或分层输出的视图引擎交错。
在 Spark 示例之一中,有一种方法可以解决这个问题,但有点棘手。
The
and
and the <test if=""> variation should all work and produce nearly identical code:
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.
尝试使用测试 if="" 语法
Try with the test if="" syntax
感谢 Louis 在 Twitter 上的帮助,问题是被调用的部分是 .ascx 文件而不是 .spark 文件。 我尚未删除旧的、未转换的 .ascx 文件。 删除 Reply.ascx 后,一切都按预期进行。
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.