Ajax.BeginForm easily allows you to work add HTML code returned by view to page. It is easy to do by defining Ajax option UpdateTargetId
But there are only three options how received HTML content should be placed to view (InsertionMode AjaxOption):
BUT! It is not possible to do with Sitecore MVC when your view is added to some placeholder on page. Even if you wrap your view with some element
There is one solution how you are able to avoid it. Insertion HTML received by Ajax is controlled by jquery.unobstrusive-ajax.js. It is JavaScript file and you can easily modify it. For Sitecore MVC views I suggest to add additional insertion mode:
UpdateTargetId ="DivContainer"
But there are only three options how received HTML content should be placed to view (InsertionMode AjaxOption):
- InsertionMode.InsertAfter
- InsertionMode.InsertBefore
- InsertionMode.Replace
With ASP.Net MVC it is ok. You can control place where you are rendering your partial view:
$(update).html(data); //jquery.unobstrusive-ajax.js
<div id="DivContainer">Then you are able to set UpdateTargetId = "DivContainer" and your form will be completely replaced.
@Html.Action("Action","Controller")
</div>
BUT! It is not possible to do with Sitecore MVC when your view is added to some placeholder on page. Even if you wrap your view with some element
<div id="DivContainer">you’ll get a lot of wrapped divs after few updating of form:
@using (Ajax.BeginForm("Action", "Controller", null, new AjaxOptions { UpdateTargetId = "DivContainer", InsertionMode = InsertionMode.Replace ….
</div>
<div id="DivContainer">It can break your design of JavaScript on page.
<div id="DivContainer">
<div id="DivContainer">
@using (Ajax.BeginForm("Action", "Controller", null, new AjaxOptions { UpdateTargetId = "DivContainer", InsertionMode = InsertionMode.Replace ….
</div>
</div>
</div>
There is one solution how you are able to avoid it. Insertion HTML received by Ajax is controlled by jquery.unobstrusive-ajax.js. It is JavaScript file and you can easily modify it. For Sitecore MVC views I suggest to add additional insertion mode:
mode = (element.getAttribute("data-ajax-mode") || "").toUpperCase();
$(element.getAttribute("data-ajax-update")).each(function (i, update) {
var top;
switch (mode) {
case "BEFORE":
top = update.firstChild;
$("<div />").html(data).contents().each(function () {
update.insertBefore(this, top);
});
break;
case "AFTER":
$("<div />").html(data).contents().each(function () {
update.appendChild(this);
});
break;
case "REPLACEWITH":
$(update).replaceWith(data);
break; default:
$(update).html(data);
break;
}
});
It will replace whole HTML element, but not only content of it.
And also you should pass corresponding html attribute to your form:
For future: I plan to implement my own Ajax.BeginForm helper with similar abilities targeted on Sitecore.
case "BEFORE":
top = update.firstChild;
$("<div />").html(data).contents().each(function () {
update.insertBefore(this, top);
});
break;
case "AFTER":
$("<div />").html(data).contents().each(function () {
update.appendChild(this);
});
break;
case "REPLACEWITH":
$(update).replaceWith(data);
break; default:
$(update).html(data);
break;
}
});
It will replace whole HTML element, but not only content of it.
And also you should pass corresponding html attribute to your form:
@using (Ajax.BeginForm("Action", "Controller", null, new AjaxOptions {Now you can do not worry about nested containers after Ajax updating of views on your Sitecore MVC application.
UpdateTargetId = "DivContainer"}new { enctype = "multipart/form-data", data_ajax_mode = "replacewith" }
For future: I plan to implement my own Ajax.BeginForm helper with similar abilities targeted on Sitecore.
No comments:
Post a Comment