Accessing currentPage in controller called by JavaScript.

Vote:
 

I'm calling this action in my controller:

Snippet

public async Task<>ActionResult> ApplySearch(ProductSearchPage currentPage, QueryJsonObject jsonObject)
{
    //currentPage is always null.
}

It is called through this JavaScript:

Snippet
$.ajax({
    type: "POST",
    url: "@Url.Action("ApplySearch""Product/ProductSearch")"// the URL of the controller action method
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(queryObject), // optional data
    success: function (result) {
        alert("Yay!");
    },
    error : function(req, status, error) {
        alert(error);
    }
});


How do I pass along the page so that I can access it in my action?

Thank you!


#181194
Edited, Aug 11, 2017 11:34
Vote:
 

The easiest way is to rely on the Episerver routing, and the in-built modelbinder (IMO laughing)

Example:

  • You have an instance of your ProductSearchPage content type created under the url /products/product-search.
  • Your ProductSearchPageController has the action ApplySearch method within it.

Make your javascript request to the url /products/product-search/ApplySearch - rather than rely on the default MVC routing of {controller}/{Action} which in this case would be ProductSearchPage/ApplySearch. Although both of these requests will be routed correctly, the currentPage object won't be bound as Episerver can't work out the context of the request, ie what content the request should be mapped to.

This appraoch assumes that you will always know the editorially controlled url of your ProductSearchPage, but you could easily get around that by having a content reference stored or 'locking' down the ability for editors to change the ProductSearchPage url.

#181195
Aug 11, 2017 13:29
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.