Scenario:
In our project we have a requirement that load items in pageload by ajax call and if there are more items, click on "LoadMore" button then load rest of the items by the same ajax call.
Issue:
The items which are loaded in page load are editable in experience editor.But the items which are loaded by "LoadMore" button are not editable.
Kindly advise on this.
Controller:
public class CategoryController : Controller
{
CategoryRepository categoryRepository = new CategoryRepository();
public ActionResult Index()
{
var categoryViewModel = new CategoryViewModel();
categoryViewModel.ContextDatabase = PageContext.Current.Database.Name;
categoryViewModel.ContextItem = PageContext.Current.Item;
return View(categoryViewModel);
}
public PartialViewResult GetCategories(int size, string itemID, string itemDB)
{
var model = categoryRepository.GetCategories(itemID, itemDB).Skip(size).Take(9);
return PartialView("_Category", model);
}
}
View:
@model CategoryViewModel
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var loadCount = 0;
var itemId = $("#contextItemId").val();
var itemDb = $("#contextDb").val();
LoadCategoires(loadCount, itemId, itemDb);
loadCount += 1;
$("#loadMore").click(function () {
LoadCategoires(loadCount, itemId, itemDb);
loadCount += 1;
});
});
function LoadCategoires(loadCount, itemId, itemDb) {
$.ajax({
url: '/api/sitecore/Category/GetCategories',
type: 'POST',
dataType: 'html',
async: true,
data: { size: loadCount * 9, itemID: itemId, itemDB: itemDb },
success: function (data) {
if (data.length !== 0) {
$("#loadMore").show();
$("#catContainer").append(data);
}
else {
$("#loadMore").hide();
}
},
error: function (data) {
alert(data.responseText);
}
});
}
</script>
<div class="row" id="catContainer">
</div>
<input type="hidden" value="@Model.ContextItem.ID.ToString()" id="contextItemId" />
<input type="hidden" value="@Model.ContextDatabase" id="contextDb" />
<div class="col-md-12">
<button id="loadMore" style="display:none;" class="center-block" value="Load More">Load More</button>
</div>
Partial View:
@model IEnumerable<Category>
@foreach (var item in Model)
{
<div>
<h4 class="text-center" style="font-weight: 600; margin-bottom: 1px;">@Html.Sitecore().Field("Title", item.InnerItem)</h4>
</div>
}