Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8071851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:49:32+00:00 2026-06-05T13:49:32+00:00

I have an ASP .NET MVC application, additonally I am using Knockout 2.0.0. I

  • 0

I have an ASP .NET MVC application, additonally I am using Knockout 2.0.0. I created a partial view which I would like to render to the page using knockout. The partial needs to be rendered within a Knockout foreach statement. I am unable to get the knockout HTML binding to work, and so I’m currently using a hack to put the html into the div using JQuery.

There is a lot of html on this page, so it’s not possible to post all of the source code, so I will try and post the pertinent code:

<div data-bind="foreach:issues">
    @* SNIP - A lot of other html here*@
    <div id="myPartialDiv" data-bind="html: $parent.getHtml(issueId())">
    </div>  
</div>

Further down I have the following javascript function on my KO View Model (I have commented out my hack and included the code that returns HTML):

   var getHtml = function (issueId) {
              var baseUrl = '@Url.Action("GetHtmlAction","MyController")';
              $.ajax(
                {
                    type: "POST",
                    url: baseUrl,
                    data: "&issueId=" + issueId,
                    success: function (data) {
                        //$('#mypartialDiv').html(data);                          
                        return data;
                    },
                    error: function (req, status, error) {
                        //$('#myPartialDiv').html('Something went wrong.');
                        return 'Something went wrong.'
                    },
                    dataType: "text"
                });
         }

The code above results in no data being rendered to the page. USing Chrome debug tools, I see that there are no javascript errors occuring, and knockout is simply not binding the html of the div to the results returned from the getHtml function.

What am I doing wrong?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:49:34+00:00Added an answer on June 5, 2026 at 1:49 pm

    As Miroslav Popovic explains, the problem is that the AJAX request is asynchronous, so the return data is ignored and there is no return value from your call to getHtml.

    I would suggest using a custom binding that handles the asynchronous HTML loading (I’ve put a working example here).

    This works by taking 2 parameters to the asyncHtml: a function to call that takes a success callback as it’s final parameter (plus any other parameters) and an array of the parameters that need to be passed to that function.

    <div id="myPartialDiv" data-bind="asyncHtml: { source: getHtml, params: [123] }">Loading...</div>
    

    The custom binding then grabs these values, concats a custom callback onto the parameters that are passed to it, and calls the specified function:

    ko.bindingHandlers.asyncHtml = {
        init: function(element, valueAccessor) {
            var value = ko.utils.unwrapObservable(valueAccessor());
            var parameters = value.params.concat([function(data) {
               $(element).html(data); 
            }]);
            value.source.apply(null, parameters);
        }
    };
    

    Finally we can re-implement our view model HTML-retrieving method to make the POST call and invoke the new success handler:

    var ViewModel = function() {
        this.getHtml = function(issueId, callback) {
            $.ajax(
                {
                    type: "POST",
                    url: "/echo/html/",
                    data: {
                            html: "<p>server response " + issueId + "</p>",
                            delay: 1
                        },
                    success: callback,
                    dataType: "text"
                });            
        };
    };
    

    Note: for this example I am using the jsFiddle echo to post back a random response

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an asp.net mvc application that we would like to integrate a FileSystemWatcher
I have an ASP.NET MVC application which is using Linq to SQL classes placed
I have a ASP.Net MVC application which serves user pages with URL like -
I have an ASP.NET MVC application using Authorization Attributes on Controllers and Actions. This
I have a ASP.NET MVC application using NHibernate and the application runs fine when
I have an ASP.Net MVC application and I'm using Forms authentication in SQL Server.
I have an asp.net mvc application. When button is clicked (submit button) I would
We have ASP.NET MVC 3 application which is integrated with Google Checkout API .
I have an asp net mvc application which is on multiple tp level domains:
I have a ASP.NET MVC application. The code in the aspx is like that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.