使用ajax调用返回部分视图

时间:2016-04-03 10:17:39

标签: sitecore sitecore7 sitecore8 sitecore-mvc sitecore8.1

我想在Sitecore的ajax调用中返回一个Partial View,我已经在使用Web API,我可以返回JSON对象,但我想用HTML返回视图:

[HttpGet]
public ActionResult StoreSearchResultsPartial()
                        {      
                  return PartialView("/views/components/StoreSearchResults.cshtml");
                }

我的ajax电话:

  var ajaxUrl = "/api/sitecore/components/StoreSearchResultsPartial"
        $.ajax({
            type: "GET",
            url: ajaxUrl,
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                $("#searchResults").html(result);
            },
            error: function (result) {

            }
        });

我正在使用Web API来实现其他功能,所以我添加了我的路由:

public void Process(PipelineArgs args)
    {
        var config = GlobalConfiguration.Configuration;

        RouteTable.Routes.MapHttpRoute(
                        name: "NamedActionApi",
                        routeTemplate: "api/{controller}/{action}/{id}",
                        defaults: new { id = RouteParameter.Optional }
                        ).RouteHandler = new SessionRouteHandler();

        config.Routes.MapHttpRoute("DefaultApiRoute",
                                 "api/{controller}/{id}",
                                 new { id = RouteParameter.Optional });



        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}

当我评论此路由时,一切正常。

1 个答案:

答案 0 :(得分:0)

现在工作正常,我将路由改为以“ api ”开头,所以我将此api/{controller}/{action}/{id}更改为mywebapi/{controller}/{action}/{id},然后我就能够致电:/api/sitecore/components/StoreSearchResultsPartial