0

I am using MVC knockout, with View Model on single js file, on a button click i am loading a partial view that is already having data-bind="text: type" done. but the view model is not able to bind the elements as it is in partial view and its is loading on a button click...is there any way that we can using bind data after pager render.

Check code below to load partial view and get data for partial view:-

$('#btnCreateTask').click(function () {
    var url = getAppPath() + 'Home/CreateTask';
    $('#midsection').load(url);
    var url = getAppPath() + 'Task/GetTaskFormDetails';
    $.ajax({
        url: url,
        cache: false,
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: {},
        success: function (data) {
            viewModel.task.TaskType.push.apply(viewModel.task.TaskType, data["objType"]);
Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Yogesh Duggal
  • 21
  • 2
  • 5
  • http://stackoverflow.com/questions/11066732/knockout-data-bind-on-dynamically-generated-elements – tcigrand Apr 15 '15 at 14:49
  • You could also use a template instead of a partial view: http://knockoutjs.com/documentation/template-binding.html – Jacques Bronkhorst Apr 15 '15 at 15:42
  • 1.you can try hiding the `div` on-load & making it visible on click (my preferred way). 2.if you are dynamically added html to div you need to `re-apply bindings` on parent div . cheers – super cool Apr 16 '15 at 09:34

1 Answers1

0

When using MVC to load partials via jQuery.load() into your application dynamically, you must understand that the elements you are bringing in are not registered with knockout. When you initialize a page you invoke knockout with ko.applybindings(). Once. Any further DOM manipulation must be a byproduct of knockout's bindings.

One of our projects is a huge Enterprise application and we load all possible partials into our View(s) on load and control their visibility by formatting them as either a knockout template or by just having a knockout visibility binding.

With either option, you are loading the DOM elements on the initial load. The added overhead is minimal and will prevent a call back to the server.

beauXjames
  • 8,222
  • 3
  • 49
  • 66