ASP.NET MVC2 使用自己的控制器渲染部分视图
我有以下部分视图 NewsSummary.ascx 用于显示新闻文章摘要列表:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<AkwiMemorial.Models.Article>>" %>
<table>
<% foreach (var item in Model as IEnumerable<AkwiMemorial.Models.Article>)
{ %>
<tr>
<td>
<strong>
<%= Html.Encode(String.Format("{0:MMMM dd yyyy}", item.DateCreated)) %>
</strong>
</td>
<td>
<%= Html.Encode(item.Abstract) %>
</td>
<td>
<a class="link1"> <%= Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%></a>
</td>
</tr>
<% } %>
</table>
My HomeController 获取要显示的文章,并在呈现之前在 Index.aspx 的 ViewData 字典中创建一个条目。然后使用 Html.RenderPartial 在 Index.aspx 中呈现部分视图,并将新闻文章列表作为其模型传递,如下所示:
<% Html.RenderPartial("NewsSummary", ViewData["news"]); %>
现在,我已经有了一个 NewsController,它可以通过给定的 Id 获取新闻文章。使用如下代码,单击 NewsSummary 部分视图中每篇文章摘要的“详细信息”链接会调用我的 HomeController 中名为“详细信息”的操作。如何将此操作绑定到 NewsController 中的 Details 方法。我研究了 Html.RenderAction 但无法确定它在这种情况下如何为我工作。
有什么想法或者我是否以错误的方式处理这个问题?
I have the following partial view NewsSummary.ascx used to display a list of news article summaries:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<AkwiMemorial.Models.Article>>" %>
<table>
<% foreach (var item in Model as IEnumerable<AkwiMemorial.Models.Article>)
{ %>
<tr>
<td>
<strong>
<%= Html.Encode(String.Format("{0:MMMM dd yyyy}", item.DateCreated)) %>
</strong>
</td>
<td>
<%= Html.Encode(item.Abstract) %>
</td>
<td>
<a class="link1"> <%= Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%></a>
</td>
</tr>
<% } %>
</table>
My HomeController fetches articles to be displayed and creates an entry in the ViewData dictionary of Index.aspx before it is rendered. Partial view is then rendered within Index.aspx using Html.RenderPartial with the lists of news articles passed as its model as follows:
<% Html.RenderPartial("NewsSummary", ViewData["news"]); %>
Now, I already have a NewsController that fetches a news article by a given Id. With the code as as, clicking on the Details link of each article summary in the NewsSummary partial view invokes an action in my HomeController called Details. How do I tie this action to the Details method in my NewsController. I looked into Html.RenderAction but could not determine how it will work for me in this scenario.
Any ideas or am I approaching this the wrong way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将控制器指定为
Html.ActionLink
的参数,如下所示:You should specify the controller as a parameter to
Html.ActionLink
like this: