开发者

Html.ActionLink in Partial View

I am using the following code in my master page:

<%  Html.RenderAction("RecentArticles","Article"); %>

where the RecentArticles Action (in ArticleController) is :

[ChildActionOnly]
    public ActionResult RecentArticles()
    {
        var viewData = articleRepository.GetRecentArticles(3);

        return PartialView(viewData);
    }

开发者_运维问答and the code in my RecentArticles.ascx partial view :

<li class="title"><span><%= Html.ActionLink(article.Title, "ViewArticle", new { controller = "Article", id = article.ArticleID, path = article.Path })%></span></li>

The problem is that all the links of the articles (which is built in the partial view) lead to the same url- "~/Article/ViewArticle" . I want each title link to lead to the specific article with the parameters like I'm setting in the partial view.

Thanks.


I think your not using the ActionLink correctly. Change the ActionLink code to:

Html.ActionLink(
    article.Title,
    "ViewArticle",
    "Article",   // put the controller here
    new
    {
        id = article.ArticleID,
        path = article.Path 
    },
    null)

Notice the null at then end.

EDIT: Why are you using [ChildActionOnly] in your controller? Since it is an MVC 2 feature I am assuming that you are using MVC2? Try removing it and check out the following article:

http://www.davidhayden.me/2009/11/htmlaction-and-htmlrenderaction-in-aspnet-mvc-2.html

I think the issue has to do with your partial not rendering. I would start by just trying to verify that your partial is rendering properly. Once you confirm that start to debug why the partial is not outputing.


I was able to solve the problem by using the following call in my RecentArticles action:

return PartialView("~/Views/Shared/Article/RecentArticles.ascx", viewData);

It seems like the partial view was not being rendered at all,

Thanks !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜