Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am trying to display data simply from an array to the home page. I am following a tutorial but the data is not displaying. Can anyone help me what i am doing wrong. what i have done so far is given bellow.

Index.cshtml Contains:


<pre lang="text"><table class="table table-striped table-bordered table-condensed">
    <tr> 
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
    </tr>
    <tbody data-bind="foreach: Profiles">
        <tr">
            <td data-bind="text: FirstName"></td>
            <td data-bind="text: LastName"></td>
            <td data-bind="text: Email"></td>
        </tr>
    </tbody>
</table>
<script src="~/Scripts/Contact.js"></script>





And the view _Layout.cshtml contains:


HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title - Contact manager</title>
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/knockout")
    @Styles.Render("~/Content/bootstrap.css")
    @Scripts.Render("~/bundles/modernizr")
    @RenderSection("scripts", required: false)
</head>
<body>

    <div class="container-narrow">
        <div class="masthead">
            <ul class="nav nav-pills pull-right">
                
            </ul>
            <h3 class="muted">Contact Manager</h3>
        </div>
        <div id="body" class="container">
            @RenderSection("featured", required: false)
            <section>
                @RenderBody()
            </section>
        </div>
        <hr />
        <div id="footer">
            <div class="container">
                <p class="muted credit">© @DateTime.Now.Year - Design and devloped by IC.</p>
            </div>
        </div>
    </div>

</body>
</html>


What I have tried:

Contact.js contains:

var DummyProfile = [
{
"ProfileId": 1,
"FirstName": "Anand",
"LastName": "Pandey",
"Email": "anand@anandpandey.com"
},
{
"ProfileId": 2,
"FirstName": "John",
"LastName": "Cena",
"Email": "john@cena.com"
}
]

var ProfilesViewModel = function () {
var self = this;
var refresh = function () {
self.Profiles(DummyProfile);
};

// Public data properties
self.Profiles = ko.observableArray([]);
refresh();
};

ko.applyBindings(new ProfilesViewModel());
Posted
Updated 25-Jul-16 22:54pm
v3
Comments
TechMocktail 26-Jul-16 11:27am    
Does your bundle contains knockout.mapping.js script?
Imran Chohan 26-Jul-16 11:29am    
I think no. I have pasted the code which i am using for this
Suvabrata Roy 18-Oct-16 8:29am    
This will help you : http://knockoutjs.com/documentation/foreach-binding.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900