如何嵌套视图?
我目前在视图中有一个 DIV,我在运行时(通过 jQuery)使用 .load() 为另一个控制器创建视图。这是可行的,但是当我查看页面时,div 是空的(当然)。我这样做是因为我还没有弄清楚如何在视图中嵌入视图。
我需要的是某种包含声明。我找到了 @RenderPage() 但不会发出控制器方法调用。
I currently have a DIV inside a view, which I .load() at runtime (via jQuery) with the create view for another controller. this works but when I look at the page the div is empty (of course). I'm doing this because I haven't figure out how to embed a view within a view.
what I need is some kind of inclusion statement. I found @RenderPage() but that won't issue a controller method call.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Html.Action 将允许您在进行该调用的视图中嵌套视图 - 通常您只会在返回部分视图的方法上执行此操作,但这将在这里完成工作。
请参阅 Html.Action 与 Html.RenderAction
Html.Action will allow you to nest a view inside of the view making that call - typically you would only do that on methods that return partial views, but that will get the job done here.
See Html.Action vs Html.RenderAction
在我看来,这是 MVC 最难的事情之一。您可以使用
,但这仅适用于仅显示视图,不适用于可编辑视图。对于这些,特别是在主模型有一个您想要为其提供编辑器的集合的(常见)用例中,您需要实现类似于 Jarret 在这里推荐的内容:
http://jarrettmeyer.com/post/2995732471/nested-collection-models-in-asp-net- mvc-3
This is one of the hardest things about MVC in my opinion. You can use
but this only works for display-only views, not so much editable views. For those, particularly in the (common) use case where the main model has a collection that you want to provide editors for, you need to implement something like what Jarret recommends here:
http://jarrettmeyer.com/post/2995732471/nested-collection-models-in-asp-net-mvc-3
实际上,我使用 jQuery.Post 访问我的控制器,并返回一个 PartialView。
然后,我获取返回的 HTML 并使用 jQuery 将该 html 放入目标 div 中。
控制器代码
jQuery / Javascript
I actually go to my controller using jQuery.Post and I return a PartialView.
I then take the returned HTML and use jQuery to place that html into the target div.
Controller code
jQuery / Javascript
如果您只是希望在视图中嵌入“视图”,则可以使用部分概念来实现此目的,请参阅
Html.Partial()
。例如,采用标准 ASP.NET 表示法:
<%= Html.Partial(stringpartialViewName) %>
或使用 Razor 语法:
@Html.Partial(stringpartialViewName)
例如:
<%= Html.Partial("_myView") %>
(这将是_myView.ascx
在您的Views\
或Shared
文件夹中,祝您好运。
If you are just simply wishing to embed a 'view inside a view', you can accomplish this with the concept of partials, see
Html.Partial()
.For example in standard ASP.NET notation:
<%= Html.Partial(string partialViewName) %>
or with Razor syntax:
@Html.Partial(string partialViewName)
For example:
<%= Html.Partial("_myView") %>
(which would be_myView.ascx
in yourViews\<action>
orShared
folder.Good luck.