This is a migrated thread and some comments may be shown as answers.

Pass Model with DataSource to MVC Method using Angularjs

1 Answer 47 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Beryl
Top achievements
Rank 1
Beryl asked on 16 Jun 2017, 11:08 PM

I have a dropdownlist on the Add Record template on a detail Grid.  I need to pass details of the parent model with the read method of the dropdownlist datasource.  Here is my code:

  $scope.detailGridOptions = function (dataItem) {
      return {
          dataSource: {
              type: 'aspnetmvc-ajax',
              transport: {
                  read: "/SSQV4/SSQV5/Operator/GetTaggedContractorsByMajor"
                  ,
                  create: "/SSQV4/SSQV5/Operator/AddContractorToTag"
              },
              serverPaging: true,
              serverSorting: true,
              serverFiltering: true,
              schema: {
                  data: "Data",
                  total: "Total",
                  model: {
                    id: "CompanyID",
                    fields: {
                        CompanyID: { editable: true, nullable: false, type: "number" },
                        TagID: { editable: true, nullable: true, type: "number" },
                        vchCompanyName: { editable: false, nullable: false, type: "string"},
                        Date: { editable: false, nullable: false, type: "date" },
                        AddedBy: { editable: false, nullable: false, type: "string" }
              }
          }
              },
              pageSize: 5,
              filter: { field: "TagID", operator: "eq", value: dataItem.TagID }
          },
          scrollable: false,
          sortable: true,
          pageable: true,
          editable: "popup",
          edit: function (e) {
              var grid = $scope.tagsGrid;
              var uid = e.model.uid;
              var selector = kendo.format("tr[data-uid='{0}']", uid);
              var currentGridElement = $(selector).closest("[data-role=grid]");
              var parentRow = currentGridElement.closest(".k-detail-row").prev();
              var parentModel = grid.dataItem(parentRow);
              e.model.TagID = parentModel.TagID;
          },
          toolbar: ["create"],
          columns: [
          {
              field: "TagID"
            , hidden: true
          }
          ,
              {
                  field: "CompanyID",
                  title: "CompanyID"
                  , editor: companyDropDownEditor
              }
          ,
            {
                field: "vchCompanyName",
                title: "Company Name"
            }
            ,
              {
                  field: "Date",
                  title: "Date Added"
              }
              ,
              {
                  field: "AddedBy",
                  title: "Added By"
              }
          ]

      };
  };

  function companyDropDownEditor(container, options) {
      var grid = $scope.tagsGrid;
      var uid = options.model.uid;
      var selector = kendo.format("tr[data-uid='{0}']", uid);
      var currentGridElement = $(selector).closest("[data-role=grid]");
      var parentRow = currentGridElement.closest(".k-detail-row").prev();
      var parentModel = grid.dataItem(parentRow);
     
      

      $('<input required name="' + options.field + '"/>')
          .appendTo(container)
          .kendoDropDownList({
              autoBind: true,
              dataTextField: "CompanyName",
              dataValueField: "CompanyID",
              template:  "#=CompanyName#",
              dataSource: {
                  transport: {
                      read: "/SSQV4/SSQV5/Operator/GetUntaggedContractors",
                      data: {contractor: parentModel}
                  }
              }
              
          });
  }

Here is my MVC Action Method:

        public async Task<ActionResult> GetUntaggedContractors([DataSourceRequest] DataSourceRequest request,
            ContractorTagModel contractor)
        {
            majorId = UserInfo.intMajorID;
            var tagId = contractor.TagID;
            var contractors = await CompanyClient.GetUntaggedContractors(majorId, tagId);
            return Json(contractors, JsonRequestBehavior.AllowGet);
        }

When the Action Method is hit, the model is always empty.  I actually don't need the entire model, just the TagID, but I don't get anything but zeros in my model.  Any assistance is greatly appreciated!

 

1 Answer, 1 is accepted

Sort by
0
Joana
Telerik team
answered on 20 Jun 2017, 03:41 PM

Hello Beryl,

Unfortunately, I am not able to diagnose the issue based on the code. Could you please send us a sample isolated project where we could observe the issue with the model? You might find useful these articles:

Regards,
Joana
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
DropDownList
Asked by
Beryl
Top achievements
Rank 1
Answers by
Joana
Telerik team
Share this question
or