TAGS :Viewed: 3 - Published at: a few seconds ago

[ MVC 3 Ajax returning partial view of area ]

I am trying to return a partial view via ajax. My code likes below and it works fine when partial view is not in an area.

public ActionResult SearchLogsAjax(string searchParams)
    // Do some searching
    return PartialView("LogResults", searchResults);

Partial View is rendered when I first open the page. The problem occurs when I press the search button which makes an ajax call to the ActionMethod. If it is in Admin area I get the classic view not found error.

The partial view 'LogResults' was not found or no view engine supports the searched locations. The following locations were searched:


Why it is not looking under Admin area which is the current area? Is there any way for me to specify the area name I wanted, by something like new { area = "Admin" }

On the other hand when I move mouse over the partial view name in the code, ReSharper shows the view which i expected.

enter image description here

Answer 1

You can specify the area name you want, by using the RouteValuesDictionary parameter and syntax same as you suggested

new { area = "Admin" }

But the trickier part to me is getting the proper method signature (meaning the signature that I intend to be using) because there are more than a couple of choices and it is EASY to get the wrong signature and still be syntactically correct.

In this case I think you want the signature:

public static MvcHtmlString ActionLink(
    this AjaxHelper ajaxHelper,
    string linkText,
    string actionName,
    RouteValueDictionary routeValues,
    AjaxOptions ajaxOptions

So modify the Ajax.ActionLink you currently have (which you did not post) to something like this:

@Ajax.ActionLink( "Link Text", "YourAction", "YourController", new { area = "YourArea" }, new AjaxOptions() { *the ajaxoptions you've been using* } )

Hope that helps.