Boris Todorov
Boris Todorov

Reputation: 430

Cannot update value in Kendo-grid - cannot read 'data' property of undegined

I am updating Kendo-grid when the selected value in option HTML element is changed. But when I try to update the value in the grid inline in the browser's console is printed "kendo.web.min.js:13 Uncaught TypeError: Cannot read property 'data' of undefined".

What Did I wrong?

< script >
  $(document).ready(function() {
  
  // on changing zone option selector
    $('#SelectProviderZoneForPrices').change(function (e) {
      var zoneId = $(this).val();
      var productId = $('#SelectProviderForPrices').val();

      $.ajax({
        type: "GET",
        url: "@Html.Raw(Url.Action("LoadZoneWeights", "ShippingZonableByWeight"))",
        success: loadPrices, // loads the shipping providers which delivers by zones
        dataType: 'json',
        data: { ProviderId: productId, ZoneId: zoneId }});
    });

    // loads prices
    function loadPrices(data) {
      var grid = $('#shippingProviderWeightPrice-grid').getKendoGrid();
      grid.dataSource.data(data.Data); // my backend returns DataSourceResult
      grid.refresh();
    }
  
    // load Kendo-grid
    $("#shippingProviderWeightPrice-grid").kendoGrid({
      dataSource: {
        type: "json",
        transport: {
          read: {
            url: "@Html.Raw(Url.Action("ActionMethod", "Controller"))",
            type: "GET",
            dataType: "json"
          },
          update: {
            url: "@Html.Raw(Url.Action("ActionMethod", "Controller"))",
            type: "POST",
            dataType: "json"
          }
        },
        schema: {
          total: "Total",
          errors: "Errors",
          model: {
            fields: {
              ProviderWeightsId: {
                editable: false,
                visible: false,
                type: "number"
              },
              ZoneId: {
                editable: false,
                visible: false,
                type: "number"
              },
              ZoneName: {
                editable: false,
                visible: false,
                type: "string"
              },
              WeightFrom: {
                editable: false,
                visible: true,
                type: "number"
              },
              WeightTo: {
                editable: false,
                visible: true,
                type: "number"
              },
              Price: {
                editable: true,
                visible: true,
                type: "number"
              }
            }
          }
        },
        requestEnd: function(e) {
          if (e.type == "update") {
            this.read();
          }
        },
        error: function(e) {
          display_kendoui_grid_error(e);
          this.cancelChanges();
        },
        serverPaging: true,
        serverFiltering: true,
        serverSorting: true
      },
      pageable: {
        refresh: true,
        numeric: false,
        previousNext: false,
        info: false
      },
      editable: {
        confirmation: true,
        mode: "inline"
      },
      scrollable: false,
      columns: [{
        field: "WeightFrom",
        title: "Weight From",
        format: "{0:n3}",
        width: 100
      }, {
        field: "WeightTo",
        title: "Weight To",
        format: "{0:n3}",
        width: 100
      }, {
        field: "Price",
        title: "Price",
        format: "{0:n3}",
        width: 100
      }, {
        command: [{
          name: "edit",
          text: "Edit"
        }]
      }]
    });
  }); < /script>
<fieldset>
  <legend><strong>Manage Weights</strong>
  </legend>
  <table class="adminContent">
    <tr>
      <td class="adminTitle">Select Provider</td>
      <td class="adminData">
        <select id="SelectProviderForPrices" name="ProviderId">
          <option value="0">- Select Provider -</option>
        </select>
      </td>
    </tr>
    <tr>
      <td class="adminTitle">Select Zone</td>
      <td class="adminData">
        <select id="SelectProviderZoneForPrices" name="ProviderId">
          <option value="0">- Select Zone -</option>
        </select>
      </td>
    </tr>
  </table>
</fieldset>
<br/>
<div id="shippingProviderWeightPrice-grid">
</div>

Upvotes: 1

Views: 3060

Answers (2)

Boris Todorov
Boris Todorov

Reputation: 430

I found the problem. I was removed the id property in my datasource-model of the grid and that caused my problems. I don't know why it is so big deal - especially when I could have objects without ID. But now it is fine.

id: "ProviderWeightsId",
fields: {
  ProviderWeightsId: { editable: false, visible: false, type: "number" },
  ZoneId: { editable: false, visible: false, type: "number" },
  ZoneName: { editable: false, visible: false, type: "string" },
  WeightFrom: { editable: false, visible: true, type: "number" },
  WeightTo: { editable: false, visible: true, type: "number" },   
  Price: { editable: true, visible: true, type: "number" }
}

Upvotes: 2

Polynomial Proton
Polynomial Proton

Reputation: 5135

You are not passing data to the loadPrices function.

$('#SelectProviderZoneForPrices').change(function (e) {
      var zoneId = $(this).val();
      var productId = $('#SelectProviderForPrices').val();

      $.ajax({
        type: "GET",
        url: "@Html.Raw(Url.Action("LoadZoneWeights", "ShippingZonableByWeight"))",
        success: function (data) {  
            loadPrices(data); //pass data to loadPrices function
        }
        dataType: 'json',
        data: { ProviderId: productId, ZoneId: zoneId }});
    });

Update:

Try this :

public ActionResult LoadZoneWeights(int ProviderId, int ZoneId)
{

  var zoneWeights = _shippingService.GetZonesWithWeights(ProviderId, ZoneId);


  return Json(zoneWeights , JsonRequestBehavior.AllowGet);
}

and

 schema: {
        data: function(response) {
            return response.data;
        }
    }

Upvotes: 1

Related Questions