如何将 ContentPlaceHolder 作为 ASP.NET MVC 母版页中 URL 的源
我有多个具有此模式的页面:
<iframe frameborder ="0" src="[someURL]" width="100%" height="900">
</iframe>
我想将除 URL 之外的所有内容分解到母版页中,因此我尝试了以下操作:
母版页:
<iframe frameborder ="0" src=<asp:ContentPlaceHolder ID="Url" runat="server" /> width="100%" height="900">
</iframe>
子页面:
<asp:Content ID="Content2" ContentPlaceHolderID="Url" runat="server">
"http://myURL"
</asp:Content>
但它没有似乎不起作用。我收到此错误:
在母版页中找不到 ContentPlaceHolder 'Url'
我上面有语法错误吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您想重新使用一个片段,以便视图可以指示 iFrame 的 URL 是什么,并且 Master 保存实际的 iFrame。
考虑这个潜在的解决方案:
URL 从控制器放入 ViewData。惯例是视图是愚蠢的。因此,您可以将此 iFrame 放入您的 Master 中:
这要求您的控制器知道或可以找到正在请求的视图的 URL。您可以在 Controller 方法中对此进行硬编码,或者从 web.config 中提取它。
It sounds like you want to re-use a snippet such that the View can dictate what the URL of the iFrame is, and the Master holds the actual iFrame.
Consider this potential solution:
The URL is put into ViewData from the Controller. Convention is that Views are dumb. So you could put this iFrame into your Master:
<iframe frameborder ="0" src="<%=ViewData["yourURL"] %>" width="100%" height="900"></iframe>
This requires that your Controller knows, or can find, the URL for the View that's being requested. You could hard-code this right in your Controller method, or pull it from the web.config.
我认为这不会很好地工作...您可以将 URL 传递到控制器内的 ViewData 中,然后注入吗?或者创建一些组件,根据当前 URL 从后端源中提取正确的值,或者其他什么?
HTH。
I don't think that will work very well... Could you pass along a URL into ViewData within the controller instead, and inject that? Or create some component that pulls the correct value from a backend source based on the current URL, or something?
HTH.
有更好的方法将源代码放入元素中,但如果您需要坚持使用正在使用的实现,则可以这样做...
请注意,我在内容占位符之前和之后都有引号。然后你就可以拥有看起来像这样的内容...
Intelisense 不会喜欢你正在尝试做的事情,但它会起作用。
There are better ways of getting your source into the element but if you need to stick with the implementation you are using you can do this...
Notice that I have quotes before and after the content placeholder. Then you can just have content that looks like this...
Intelisense will not like what you are trying to do but it will work.