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 8870275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:41:00+00:00 2026-06-14T17:41:00+00:00

I’ve been trying to follow the tutorials for Knockout.js in order to render a

  • 0

I’ve been trying to follow the tutorials for Knockout.js in order to render a table in my view. My controller is retrieving a list of records as an IEnumerable type and sending to the view. I’m trying to get Knockout mapping to work to display my records in an HTML Table.

My model looks like:

public class ImportItem
{
    public string LName { get; set; }
    public string FName { get; set; }
    public string HPhone { get; set; }
    public string EMailAddress { get; set; }
    public string OtherPhone { get; set; }
}

I can’t seem to pass the IEnumerable to the View and get even a simple table with just LName and FName to display. Does anyone have any sample code using knockout mapping that I can look at?

Solved Below

At the top of my view I added:

@model IEnumerable<MyProject.Models.ImportItem>
@{    
    var jsonData = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model));
}

My javascript:

$(function () {
        var viewModelJSON = ko.mapping.fromJSON('@Html.Raw(jsonData)');

        ko.applyBindings(viewModelJSON);
    });

My html binding, the $data winded up being the key to reference the unnamed array:

<table>
  <thead><tr>
    <th>Last Name</th>
  </tr></thead>
  <tbody data-bind="foreach: $data">
    <tr>
      <td data-bind="text: LName"></td>
    </tr>    
  </tbody>
</table>
  • 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-14T17:41:02+00:00Added an answer on June 14, 2026 at 5:41 pm

    The mapping plugin is intended for mapping from a javascript (JSON) object. What you can do is in the controller, serialize all the data you need to send into a JSON string and set that as an attribute of the model you send to the view:

    public class MyModel
    {
        public string JsonData { get; set; }
    }
    

    After the your data has been generated you can use a serializer (many exist, in the example below I use Json.NET) to convert it to a JSON string.

    model.JsonData = JsonConvert.SerializeObject(importItems);
    

    Then in your view, you can use the mapping plugin to map your JSON object, using json2.js for parsing the string and converting it to JSON.

    <script type="text/javascript">
        var viewModel = ko.mapping.fromJS($.parseJSON('@Html.Raw(Model.JsonData)'));
        ko.applyBindings(viewModel);
    </script>
    

    Alternatively, you could send your IEnumerable of models to the view the same way you have in your question, and serialize them with Razor in the view (rather than in the controller) before doing the javascript parsing and mapping. This would probably be the better way if you want to follow the MVC pattern as strictly as possible.

    So before the <script> block, you could open up a block of razor code and declare a variable to hold the serialized IEnumerable, so something like this:

    @{
        string jsonData = JsonConvert.SerializeObject(Model);
    }
    

    Then in the javascript block:

    var viewModel = ko.mapping.fromJS($.parseJSON('@Html.Raw(jsonData)'));
    

    To know what attributes to bind your data-bind="foreach ..." tags to, you may have to do some debugging in the javascript to inspect the viewModel object and see how exactly the mapping plugin mapped your IEnumerable<ImportItems>. I’ve only ever done this with a single model, so I’m really not sure what would happen.

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

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I have been unable to fix a problem with Java Unicode and encoding. The
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.