创建可重用的小部件
我正在使用 asp.net mvc 2.0 并尝试创建可重用的网站部分,这些部分可以动态添加到任何页面。
我遇到的问题是如何加载包含所有相关 js 和数据的部分视图?我尝试了以下方法来做到这一点:
使用部分视图并将所有 js 放入其中。在主视图中使用渲染部分。但是要初始化部分视图,我需要将模型添加到当前操作方法模型中,以便能够生成
RenderPartial("MyPartialView", Model.PartialViewModel)
。
另外,我没有地方放置填写表单所需的附加数据(例如下拉列表值、一些预定义值等)。使用RenderAction,但似乎它与RenderPartial有相同的问题,除了我不需要向任何其他模型添加任何内容。
使用
任何其他选项将不胜感激。
I`m using asp.net mvc 2.0 and trying to create reusable web site parts, that can be added at any page dynamically.
The problem I have is how to load a partial view with all related js and data? Ive tried the following ways to do that:
Use partial view and put all the js into it. In main view use render partial. But to initialize partial view I need to add model to current action method model to be able to make
RenderPartial("MyPartialView", Model.PartialViewModel)
.
Also I do not have a place to put additional data I need to fill my form(like drop down lists values, some predefined values etc).Use RenderAction, but it seems it have same problems as RenderPartial, except for I do not need to add anything to any other model.
Any other oprions are greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,RenderAction 对操作执行完整的管道,然后渲染结果 - 因此渲染的内容与浏览到操作时看到的内容相同。
我已经使用 RenderAction 在整个站点中渲染“小部件”,但在我看来,它们应该独立于渲染它们的页面,否则它们不是真正的小部件,而应该是渲染页面代码的一部分。例如,如果有一个登录表单,您总是会将用户带到可以处理信息的页面,无论他们当前位于哪个页面,因此这是一个很好的小部件。我使用它的其他方式是展示购物篮或广告。两者都不依赖于所显示的页面。
希望这会有所帮助!
As I understand it, RenderAction performs the full pipeline on the action, then renders the result - so what is rendered is the same as what you'd see if you'd browsed to the action.
I've used RenderAction to render 'widgets' throughout a site, but in my view they should be independent of the page rendering them, otherwise they're not really widgets and should be part of the rendering page's code instead. For instance, if there's a log in form, you will always take the user to a page that can process the information, no matter what page they are currently on, so this makes for a good widget. Other ways I've used it is to show a shopping basket or advertising. Neither of which are dependent on the page being shown.
Hope this helps a little!