Razor 部分视图未渲染
如果我使用以下控制器方法:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将其更改
为:
看起来没有@,该方法正在被调用,但返回值只是被丢弃。添加 @ 会使其写入响应中。
Try changing this:
to this:
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.
RenderAction
方法将操作直接写入视图并返回void
。Action
方法返回操作的内容,但不会向视图写入任何内容。写入
@something
会将something
的值打印到页面。您不能编写
@Html.RenderAction
,因为RenderAction
不会返回任何内容。编写
Html.Action(...)
(不带@
)会正常调用该方法,但不会对其返回值执行任何操作。The
RenderAction
method writes the action directly to the view and returnsvoid
.The
Action
method returns the action's contents but doesn't write anything to the view.Writing
@something
will print the value ofsomething
to the page.You cannot write
@Html.RenderAction
, sinceRenderAction
doesn't return anything.Writing
Html.Action(...)
(without@
) calls the method normally, but doesn't do anything with its return value.好的,改变从 _Layout.cshtml 调用它的方式...
重要的是要注意,@Html.RenderAction 对我不起作用。我真的很想在这里得到一些解释,因为现在学习 Razor 让我很沮丧,因为几乎没有文档,而且像这样的问题需要几分钟才能解决,却占用了我太多的时间。
OK, Changing the way the it was called from _Layout.cshtml worked...
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.