You can just use jquery ajax call and return partial view (just use local block view in shared folder).
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?
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.
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?
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?
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.