渲染MVC5剃刀局部视图 [英] Rendering partial views with Razor in MVC5

查看:135
本文介绍了渲染MVC5剃刀局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让部分鉴于MVC5使用剃刀来呈现。当我使用

I'm trying to get a partial view to render using Razor in MVC5. When I use

@{ Html.RenderPartial("ViewName", model); }

我得到分析器错误:

I get the parser error:

意外的{之后@字符。一旦code座(@if {},@ {}等),您不需要使用的体内@ {切换到code。

Unexpected "{" after "@" character. Once inside the body of a code block (@if {}, @{}, etc.) you do not need to use "@{" to switch to code.

当我删除 {} ,即:

@Html.RenderPartial("ViewName", model);

我得到的编译错误

I get the compilation error

无法隐式转换类型'无效'到'对象'。

Cannot implicitly convert type 'void' to 'object'.

我在做什么错了?

推荐答案

您还没有张贴code的范围内,但错误,当您只使用真正发生 @ 直接在code座没有任何HTML包装纸。例如:

You haven't posted the context of that code, but that error only really happens when you're using @ directly within a code block without any HTML wrappings. For example:

@if (true) {
    @{ Html.RenderPartial(...); }
}

会给你的错误,而

Would give you the error, while:

@if (true) {
    <div>
        @{ Html.RenderPartial(...); }
    </div>
}

就可以了。您还可以通过只取出code座为 Html.RenderPartial 完全,包括解决了 @

@if (true) {
    Html.RenderPartial(...);
}

这篇关于渲染MVC5剃刀局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆