asp.net MVC 部分视图控制器操作
我对 Web 应用程序开发非常陌生,我想我应该从最新的技术开始,所以我尝试立即学习 ASP.NET 以及 MVC 框架。对于 MVC 专业人士来说,这可能是一个非常简单的问题。
我的问题是分部视图是否应该有关联的操作,如果是这样,每当普通页面在分部视图上使用 RenderPartial() 时,是否会调用此操作?
I'm very new to web app development and I thought I would start with recent technology and so I'm trying to learn asp.net as-well as the MVC framework at once. This is probably a very simple question for you, MVC professionals.
My question is should a partial view have an associated action, and if so, does this action get invoked whenever a normal page uses RenderPartial()
on the partial view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
虽然您可以使用返回部分视图的操作,但不需要渲染部分视图的操作。 RenderPartial 获取部分视图并使用给定的模型和视图数据(如果提供)将其渲染到当前(父)视图中。
如果您使用 AJAX 加载/重新加载页面的一部分,您可能需要一个返回部分视图的操作。在这种情况下,不需要返回完整视图,因为您只想重新加载页面的一部分。在这种情况下,您可以让操作仅返回与页面该部分相对应的部分视图。
标准机制
在普通视图中使用部分视图(无需任何操作)
Ajax 机制
通过 AJAX 重新加载页面的一部分(注意部分在初始页面加载时内联呈现)
AJAX 控制器
While you can have an action that returns a partial view, you don't need an action to render a partial view. RenderPartial takes the partial view and renders it, using the given model and view data if supplied, into the current (parent) view.
You might want an action that returns a partial view if you are using AJAX to load/reload part of a page. In that case, returning the full view is not desired since you only want to reload part of the page. In this case you can have the action just return the partial view that corresponds to that section of the page.
Standard mechanism
Making use of partial view within a normal view (no action needed)
Ajax mechanism
Reloading part of a page via AJAX (note partial is rendered inline in initial page load)
Controller for AJAX
接受的答案是完全正确的,但我想补充一点,您可以使用 jQuery load 加载部分视图。如果您不想考虑并发性,则需要较少的配置。
The accepted answer is completely correct, but I want to add that you can load your partial view using jQuery load. Less configuration needed, if you don't want to consider concurrency.
我能够用这种逻辑实现类似的目标。
在 .cshtml 内
在控制器内
就是这样。
如果您需要将值从 .cshtml 传递到操作方法,那么这是可以的。
I was able to achieve something similar with this logic.
Within the .cshtml
Within the controller
And that's it.
If you need to pass values from the .cshtml to the action method then that is possible to.
答案是否定的。但有时您需要在部分视图后面进行一些控制器操作。然后您可以创建一个返回部分视图的actionMethod。这个actionMethod可以在另一个视图中调用:
actionmethod可以看起来像:
并且视图“StockWarningsPartial.cshtml”以:开头,
以使其不会再次渲染周围的布局。
The answer is no. But sometimes you need some controller action behind a partial view. Then you can create an actionMethod wich returns a partial view. This actionMethod can be called within another view:
The actionmethod can look like:
and the view 'StockWarningsPartial.cshtml' starts with:
to make it not render your surrounding layout again.