Spark 中此 MVC 视图代码的等效语法是什么?

发布于 2024-07-25 14:15:00 字数 573 浏览 5 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停顿的约定 2024-08-01 14:15:00

and

<% 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>

变体应该全部工作并产生几乎相同的代码:

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

也许尝试输出 ${UserService.IsAuthenticated} 和 ${Model.Post.IsDeleted} 以绝对确定条件并不总是正确?


好的 - 在另一种媒体中确认这是不正确的...“回复”部分是否可能是像 Reply.ascx 或 Reply.aspx 这样的 WebForms 视图? WebForms 存在一个问题,默认情况下它的输出将直接转到当前 HttpContext 响应输出,这使得很难将这些部分与假脱机或分层输出的视图引擎交错。

在 Spark 示例之一中,有一种方法可以解决这个问题,但有点棘手。

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.

不喜欢何必死缠烂打 2024-08-01 14:15:00

尝试使用测试 if="" 语法

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

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>
奈何桥上唱咆哮 2024-08-01 14:15:00

感谢 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文