最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Call Sitecore controller method from JavaScript (MVC) - Stack Overflow

programmeradmin1浏览0评论

I have a controller with a function ShowEvents(EventCategory eventCategory). Is it possible to call this function from client-side JavaScript? I know that I can retrieve items from the database using the Sitecore.Services.Client (SCC), but is it possible to actually access methods? Maybe through the controller rendering, if that's possible?

Here is an example of a method I want to call:

public class EventListController : Controller
{
    public ActionResult ShowEvents(EventCategory eventCategory)
    {
        var repository = new EventListRepository();
        var eventPages = repository.GetEvents(eventCategory);
        var eventListViewModel = repository.GetEventListViewModel(eventPages);

        return View("/Some/Path/, eventListViewModel);
    }
}

This is on Sitecore 7.5 MVC

I have a controller with a function ShowEvents(EventCategory eventCategory). Is it possible to call this function from client-side JavaScript? I know that I can retrieve items from the database using the Sitecore.Services.Client (SCC), but is it possible to actually access methods? Maybe through the controller rendering, if that's possible?

Here is an example of a method I want to call:

public class EventListController : Controller
{
    public ActionResult ShowEvents(EventCategory eventCategory)
    {
        var repository = new EventListRepository();
        var eventPages = repository.GetEvents(eventCategory);
        var eventListViewModel = repository.GetEventListViewModel(eventPages);

        return View("/Some/Path/, eventListViewModel);
    }
}

This is on Sitecore 7.5 MVC

Share Improve this question edited Jan 5, 2016 at 9:35 tereško 58.5k25 gold badges100 silver badges150 bronze badges asked Jan 5, 2016 at 2:16 MooMoo 3,7155 gold badges25 silver badges42 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

You can Post to controllers from the client side using the format

/api/sitecore/{yourcontroller}/{action} in your case this would be /api/sitecore/eventlist/showevents passing the eventCategory as the data.

yes you can reach every function with

A separate view page named with same name of it and the view pages

will be written with RAZOR language which is posed of c# and html and surely you can write javascript within the html code. with the Asp and MVC5

here an example ::: http://www.asp/mvc/tutorials/mvc-5/introduction/getting-started

You can use the below format for call the Action method in sitecore project.

Sitecore have own route to manage the action method which is used as API. You can use it for Ajax call from the fronted.

/api/Sitecore/{controller Name}/{action method name}

Just post your data as request in data object and cosume the url in above format. It's act like API.

发布评论

评论列表(0)

  1. 暂无评论