This is a migrated thread and some comments may be shown as answers.

Change HTML in ListView elements created using a template with AJAX in ASP.NET MVC 4 application

1 Answer 141 Views
Templates
This is a migrated thread and some comments may be shown as answers.
Matyas
Top achievements
Rank 1
Matyas asked on 27 Feb 2013, 11:47 AM
Hello,

I have the following challenge:
I'm using the ListView in an ASP.NET MVC application, where the Kendo UI ListView sits in a Partial View. The items in the list view shall be created using another Partial View (so their content may be different item by item). I've tried to hook up some jQuery code, which alter the elements by using AJAX calls, but it seems that my code runs before the ListView gets rendered. The Action URL is stored in a data attribute of the generated 'div' sections.
Here is the .cshtml code for the partial view:

@model TheModel
@*@{ Layout = null; }*@
<link href="@Url.Content("~/Content/listviewitem.css")" rel="stylesheet" type="text/css" />
<script type="text/x-kendo-tmpl" id="template">
    <div class="itemclass" data-actionurl="${ActionUrl}" />
</script>
<div>
    @(Html.Kendo().ListView<TheDetailModel>()
        .Name("ListView")
        .TagName("div")
        .ClientTemplateId("template")
        .Pageable()
        .DataSource(p_DataSource => p_DataSource
            .Read(p_Read => p_Read.Action("TheDetailModel_Read", "TheModel")
            .Data("additionalData")))
        )
</div>
<script type="text/javascript">
    function additionalData() {
        return { p_TheDetailModelId: "@Model.Id" };
    }
    jQuery(function () {
        jQuery('.itemclass').each(
            function (index, element) {
                var actionUrl = jQuery(element).attr('data-actionurl');
                jQuery.ajax({
                    url: actionUrl,
                    success: function (result) {
                        jQuery(element).html(result);
                    }
                });
            }
        )
    });
</script>

Has anybody an idea how to make this work as expected?

1 Answer, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 01 Mar 2013, 09:44 AM
Hello Matyas,

The ListView is bound via Ajax so the items will be rendered after the data is received from the server. In order to execute your logic after the item elements have been added you should use the dataBound event.

Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Templates
Asked by
Matyas
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or