当我在 Html.RenderAction() 中使用 Html.ActionLink() 时,它给了我一个空链接
我有一个带有操作“Foo”的 Microsoft MVC 项目,其视图(“Foo.aspx”)包含以下行:
<%= Html.ActionLink("mylinktext1", "bar") %>
<%= Html.ActionLink<MyController>(x => x.Bar(), "mylinktext2") %>
当我从 Web 浏览器中点击此内容或从 AJAX 调用加载它时,它会正确返回:
<a href="/bar">mylinktext1</a>
<a href="/Bar">mylinktext2</a>
但是当我调用来自另一个视图的操作如下:
<% Html.RenderAction<MyController>(x => x.Foo()); %>
然后,链接将在没有目标的情况下呈现。
<a href="">mylinktext1</a>
<a href="">mylinktext2</a>
为什么会发生这种情况,我该如何解决?
I have a Microsoft MVC project with an action "Foo" whose view ("Foo.aspx") contains the lines:
<%= Html.ActionLink("mylinktext1", "bar") %>
<%= Html.ActionLink<MyController>(x => x.Bar(), "mylinktext2") %>
When I hit this from a web browser or load it from an AJAX call, it properly returns:
<a href="/bar">mylinktext1</a>
<a href="/Bar">mylinktext2</a>
But when I call the action from another view like this:
<% Html.RenderAction<MyController>(x => x.Foo()); %>
Then the links are rendered without targets.
<a href="">mylinktext1</a>
<a href="">mylinktext2</a>
Why would this be happening, and how do I work around it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道这是否是您做错的地方,但我从未将 Html.RenderAction 与返回 ASPX 视图的操作一起使用。当我调用 RenderAction 时,我确保调用返回 ASCX 视图用户控件的控制器操作。
通常,.ASPX 文件是整个页面,您不能(不应该)在另一个页面中呈现它。
所以我认为你应该将其设置为“查看用户控件”(ASCX) 并将其放在“共享”或控制器的视图文件夹中。
根据您的评论:
当然这很好。您只需将数据作为模型返回到视图/视图用户控件。当您通过 AJAX 加载它们时,您应该考虑将渲染视图实现为字符串。搜索 Google 或 Stack 以获取更多信息。您还可以为您的操作方法搜索名为 JsonPox 属性的东西 - 也在互联网上的某个地方实现。它将能够装饰您的操作方法,以便它们能够返回 HTML 视图、XML 或 JSON(如果您也可能需要)。
I don't know if that is what you are doing wrong, but I have never used Html.RenderAction with actions that return ASPX views. When I call RenderAction, I make sure that I am calling a controller action that returns ASCX View User Control.
Typically .ASPX file is an entire page and you can't (shouldn't) render this inside another page.
So I think you should make it View User Control (ASCX) and put it either in Shared or in controller's view folder.
Based on your comment:
Of course this is fine. You just return your data as model to your views/view user controls. When you load them thru AJAX, you should consider implementing Render View to String. Search the Google or Stack for more information on it. You can also search for a thing called JsonPox attribute for your action methods - also implemented somewhere on the internet. It will enable decorating your action methods so that they are able to return HTML view, XML or JSON if that's what you also might need.