Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello friends,
I'm new to MVC and knockout. Please refer the below code

Controller Code:
public string getJSON()
       {
           Product _objProduct = new Product();
           DataTable _objDT = _objProduct.getProduct();
           System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
           List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
           Dictionary<string, object> row = null;

           foreach (DataRow dr in _objDT.Rows)
           {
               row = new Dictionary<string, object>();
               foreach (DataColumn col in _objDT.Columns)
               {
                   row.Add(col.ColumnName, dr[col]);
               }
               rows.Add(row);
           }
           return serializer.Serialize(rows);
       }


View Code
<script>
    $(document).ready(function () {
        $('#divheader').append("<h2>Please place an order</h2>");
        $.ajax({
            type: "Post",
            url: "/Order/getJSON",
            data: {},
            success: function (data) {
                alert(data);
                /*function ReservationsViewModel() {
                    var self = this;
                    self.product = data;

                    self.addOrder = function () {
                        alert('in');
                        self.order.push(new orderReservation(_objModel));
                    }
                }
                ko.applyBindings(new ReservationsViewModel());*/
            }
        });
    });
</script>

<table>
    <thead>
        <tr>
            <th>Product Name</th>
            <th>Quantity/Unit</th>
            <th>Unit Price</th>
            <th>Quanitity</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <select id="ddlProduct"></select>
            </td>
            <td><label></label></td>
            <td><label></label></td>
        </tr>
    </tbody>
</table>



Can anyone tell me how can i bind the data returned by ajax call to the ddlProduct using knockout.

Thanks in advance
Posted
Comments
dhage.prashant01 15-Mar-13 8:13am    
If I try
<select id="ddlProduct" data-bind="options: ProductName"></select>
and my data contains in following format
[{"ProductID":1,"ProductName":Monitor},......]

I gives error:
Microsoft JScript runtime error: Unable to parse bindings.
Message: [object Error];
Bindings value: options: ProductName

Any Idea, where I'm goin wrong??

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