Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Ajax request MVC refresh shared block

Vote:
 

Hi,

I'm searching for a solution to refresh a shared block (Hml) when I perform an ajax request. In my block (controller) I need to retrieve some data and then I would like to replace all the html in the block view. 

Thanks.

#73824
Aug 12, 2013 10:47
Vote:
 

You can just use jquery ajax call and return partial view (just use local block view in shared folder).

#73900
Aug 14, 2013 5:44
Vote:
 

I'm actually having similar challenge, I have custom form in block, want to post it with Ajax back to Block controller's HttpPost action and as a return value I would like to get only the Partial View rendered by block controller. The thing I don't yet understand is how to call block controller directly without have the whole page controller executed? How to generate URL that maps directly to block controller instead of page controller?

#73904
Aug 14, 2013 9:05
Vote:
 

Block controller return your block view ,url is /controller/action. you can use jquery to call ajax such as

$.ajax({
                url: "/Controller/Action/",
                data: { ContactName: $("#ContactName").val(), ContactEmail: $("#ContactEmail").val(), ContactPhone: $("#ContactPhone").val(), ContactComments: $("#ContactComments").val() },
                type: "POST",
                error: function (data) {
                    $("#contact").html(data);
                    return false;
                },
                success: function (data) {
                    $("#contact").html(data);

})

 

The id contact is map to a container (div) which your block contains in.

 

#73911
Aug 14, 2013 10:28
Vote:
 

Thanks, but can't get it to work. I have block with name "CoolFormBlock". I'm using following code to get correct route/url to my block controller:

RouteTable.Routes.GetVirtualPathForPartialRouted(Model.CurrentBlock, null).GetUrl()

Which returns "/CoolFormBlock/Index?length=73" but with this url I just get 404. What am I missing here? Is there some additional magic I need to add to the block controller to make it routable?

#73913
Aug 14, 2013 10:45
Vote:
 

Thanks for the replies. I solved the routing by adding this to the global.asax:

routes.MapRoute(
"blockController", // Route name
"Blocks/{controller}/{action}", // URL with parameters
new { action = "Index" });

If I call the url /blocks/{controller}/Index a breakpoint hits in my controller. The only thing is that the block model of the Index method is null, do you know how to solve this?

#73929
Aug 14, 2013 14:49
Vote:
 

I have same Patrick issue I don't know how to prevent block model to be null. Anyone have an idea about this?

#123576
Jul 09, 2015 4:42
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.