English 中文(简体)
ASP.NET MVC2使用自己的控制器呈现局部视图
原标题:ASP.NET MVC2 rendering a partial view with its own controller

我有以下部分视图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字典中创建一个条目。然后在Index.aspx中使用Html.RenderPartial呈现部分视图,并将新闻文章列表作为其模型传递,如下所示:

<% Html.RenderPartial("NewsSummary", ViewData["news"]); %>

现在,我已经有了一个NewsController,它按照给定的Id获取新闻文章。使用如下代码,在NewsSummary部分视图中单击每篇文章摘要的Details链接会调用我的HomeController中名为Details的操作。如何将此操作与NewsController中的Details方法联系起来。我研究了Html.RenderAction,但无法确定在这种情况下它将如何为我工作。

有什么想法吗?还是我处理这个问题的方式不对?

最佳回答

您应该将控制器指定为Html.ActionLink的参数,如下所示:

<%= Html.ActionLink("Details", "Details", "News", new { /* id=item.PrimaryKey */ }, null) %>
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签