1

Could someone please let me know if there's a way to pass the current block on an ajax call?

Scenario: On initial get request ,the current block populates the view that includes a dropdown say for instance country list. Next i need an ajax call to get in the list of states based on the country id selected The ajax call i make right now gives me the country id but ,the current block is null. So is there a way to pass the current block as well as an additional parameter with country id which i can then use to filter the items from the current block values?(The data is within the current block & not database & so i need the current block)

                $.ajax({
                    type: "GET",
                    url: "/dummycontroller/getstates/",                    
                    data: {                        
                        countryid: countrid
                    },
                    success: () => {
                    },
                    error: () => {
                    }
                });


Controller code
[HttpGet]
     
public virtual ActionResult GetStates(ABCBlock currentBlock, string countryid)
        {
//Here i get the country id but i want current block as well which right now is null 
}
Is there a way to achieve this?
Ms1
  • 45
  • 5
  • There is no `currentBlock` as you're not passing any argument value. Perhaps you could include the block ID as an argument to your `GetStates()` method? It could be an `int` parameter, and then use `IContentLoader` to get the actual block instance in your action method. – Ted Nyberg Oct 12 '21 at 11:42
  • Yes & the reason for not passing is, i am unable to get the current block to pass it .Is there any way to get the current block & pass it as one of the parameter other than getting Block id & using contentloader? – Ms1 Oct 12 '21 at 13:46
  • I'm not sure what you mean by the "current block" in that context? – Ted Nyberg Oct 12 '21 at 15:46
  • current block is the block that is added on a page. This block includes the list of countries which in turn has states in the form of nested blocks & so i need to get the block as well along with country id which i can query to get the specific country details – Ms1 Oct 12 '21 at 18:12
  • to add to @TedNyberg's comment - here is more detailed sample of this approach (pass block ID to the view, then use that ID as a parameter in the AJAX request, and then load that block by ID in the controller): https://stackoverflow.com/a/37551198/2170171 – Lanorkin Oct 13 '21 at 07:52
  • thanks @Lanorkin.For the link u shared ,i had gone through this before & wanted to know if there is an other alternative to go down the route of including block id & again fetching the details through IContentLoader.This approach was my last option to go with if there is no other alternative to pass the current block itself an an additional parameter. – Ms1 Oct 14 '21 at 02:03

0 Answers0