Ali
Ali

Reputation: 2768

HTML.RenderAction from Different location?

I have [ChildActionOnly] in my Controller called _AllLinks

I can use RenderAction if _AllLinks.cshtml is in my Controller Directory but I cannot use it if i try to put it in a different directory in VIEWS folder such as the following

Html.RenderAction("_AllLinks", "Home", new { Model = Model, Area = "Core/List/" });

Where Location of _AllLinks.cshtml is Views/Core/Lists/

How can i call Html.RenderAction if PartialView called _AllLinks is in a different folder?

Cheers

EDIT

public ActionResult Index()
        {

            return View();
        }

        [ChildActionOnly]
        public ActionResult _AllLinks(List<Models.Links.MLink> Model)
        {
            return PartialView("~/Views/Core/Lists/_AllLinks", Model);
        }

Upvotes: 2

Views: 1007

Answers (2)

Shyju
Shyju

Reputation: 218732

Yes! You can. You need to explicitly specify the full path to the view.

[ChildActionOnly]
public ActionResult _AllLinks(List<Models.Links.MLink> Model)
{
    return PartialView("~/Views/Core/Lists/_AllLinks.cshtml",Model);
}

Make sure you use the full path with the file extension (.cshtml)

Also you should be calling the RenderAction with route values matching your action method signature.

<div>
    @{ Html.RenderAction("_AllLinks", "Home", new {model = Model}); }
</div>

Upvotes: 3

Backs
Backs

Reputation: 24903

My code:

public PartialViewResult Language()
{
    return PartialView("Admin/Languages/_LanguageEditView");
}

_LanguageEditView is located at Shared/Admin/Languages/ folder. I think, Shared is important for partial views.

Upvotes: 0

Related Questions