Razor 部分视图未渲染

发布于 2024-11-04 22:47:50 字数 1027 浏览 4 评论 0原文

如果我使用以下控制器方法:

public ActionResult Menu()
{
    // do stuff...

    return PartialView("viewName", navLinks);
}

在 _Layout.cshtml 中调用部分视图,如下所示:

<div id="categories">
    @{ Html.Action("Menu", "Nav"); }
</div>

使用以下 ASCX 部分视图:

<%@ Control Language="C#"
    Inherits="ViewUserController<IEnumerable<MyDataType>>" %>

<% foreach(var link in Model) { %>
    <%: Html.Route.Link(link.Text, link.RouteValues) %>
<% } %>

一切正常。耶。


但是,如果我使用以下 RAZOR 部分视图之一:

@model IEnumerable<MyDataType>

@foreach(var link in Model){
    Html.RouteLink(link.Text, link.RouteValues);
}

或者...

@model IEnumerable<MyDataType>

@{
    Layout = null;
}

@foreach(var link in Model){
    Html.RouteLink(link.Text, link.RouteValues);
}

我什么也得不到。没有抛出异常,我只是没有得到任何渲染。我知道问题不在于控制器方法(它与 ASCX 部分视图配合得很好)。

这是怎么回事?

If I use the following Controller method:

public ActionResult Menu()
{
    // do stuff...

    return PartialView("viewName", navLinks);
}

calling the partial view in _Layout.cshtml like this:

<div id="categories">
    @{ Html.Action("Menu", "Nav"); }
</div>

With the following ASCX partial view:

<%@ Control Language="C#"
    Inherits="ViewUserController<IEnumerable<MyDataType>>" %>

<% foreach(var link in Model) { %>
    <%: Html.Route.Link(link.Text, link.RouteValues) %>
<% } %>

everything works fine. Yay.


BUT, if I use either of the following RAZOR partial views:

@model IEnumerable<MyDataType>

@foreach(var link in Model){
    Html.RouteLink(link.Text, link.RouteValues);
}

or...

@model IEnumerable<MyDataType>

@{
    Layout = null;
}

@foreach(var link in Model){
    Html.RouteLink(link.Text, link.RouteValues);
}

I get nothing. there's no exception thrown, I just don't get anything rendered. I know the problem isn't with the controller method (it works just great with the ASCX partial view).

What's going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

恏ㄋ傷疤忘ㄋ疼 2024-11-11 22:47:50

尝试将其更改

@foreach(var link in Model){
    Html.RouteLink(link.Text, link.RouteValues);
}

为:

@foreach(var link in Model){
    @Html.RouteLink(link.Text, link.RouteValues);
}

看起来没有@,该方法正在被调用,但返回值只是被丢弃。添加 @ 会使其写入响应中。

Try changing this:

@foreach(var link in Model){
    Html.RouteLink(link.Text, link.RouteValues);
}

to this:

@foreach(var link in Model){
    @Html.RouteLink(link.Text, link.RouteValues);
}

It looks like without the @ the method is being called, but the return value is just being dscarded. Putting the @ causes it to be written in the response.

ㄟ。诗瑗 2024-11-11 22:47:50

RenderAction 方法将操作直接写入视图并返回 void
Action 方法返回操作的内容,但不会向视图写入任何内容。

写入 @something 会将 something 的值打印到页面。
您不能编写 @Html.RenderAction,因为 RenderAction 不会返回任何内容。

编写 Html.Action(...)(不带 @)会正常调用该方法,但不会对其返回值执行任何操作。

The RenderAction method writes the action directly to the view and returns void.
The Action method returns the action's contents but doesn't write anything to the view.

Writing @something will print the value of something to the page.
You cannot write @Html.RenderAction, since RenderAction doesn't return anything.

Writing Html.Action(...) (without @) calls the method normally, but doesn't do anything with its return value.

稚气少女 2024-11-11 22:47:50

好的,改变从 _Layout.cshtml 调用它的方式...

<div id="categories">
    @Html.Action("Menu", "Nav");
</div>

重要的是要注意,@Html.RenderAction 对我不起作用。我真的很想在这里得到一些解释,因为现在学习 Razor 让我很沮丧,因为几乎没有文档,而且像这样的问题需要几分钟才能解决,却占用了我太多的时间。

OK, Changing the way the it was called from _Layout.cshtml worked...

<div id="categories">
    @Html.Action("Menu", "Nav");
</div>

It is important to note, that @Html.RenderAction DOES NOT work for me. I'd really love some explanation here, because right now, learning Razor is frustrating me as there is little documentation, and problems like these which should take minutes to resolve, are eating up way too much of my time.

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