1

I m filling a (MANAGER)dropdownlistfor based on (DEPARTMENT)dropdownlistfor as follows. Using MVC mode SP O365 list. But I m not sure how to pass the selected value from Department dropdown to Manager dropdown.

  function onDepartmentChange() {
            var DepartmnetId = $("#Department option:selected").text();
            alert(+DepartmnetId);
           // alert("before");

            $.ajax({
                url: '@Url.Action("FillCity")',
                type: "POST",
                dataType: "JSON",
                contentType: 'application/json; charset=utf-8',
                data: {} ,
                success: function (result) {
                    $("#Manager").html("");
                    $.each($.parseJSON(result), function (i, city) {
                        $("#Manager").append
                        ($('<option></option>').val(city.CityID).html(city.CityName))
                    })
                },
                error: function () { alert("Something bad"); }
            });


 <div class="form-group">
                @Html.LabelFor(model => model.Department.Name, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @*Binding the choice field to dropdown list*@
                    @Html.DropDownListFor(model => model.Department.Name,
                   new SelectList(ViewBag.AllDepartments, "Department"),
                   "Select the Department", 
                   new { id = "Department", onchange="onDepartmentChange();" })                        
                    @Html.ValidationMessageFor(model => model.Department.Name)
                </div>
            </div>

 <div class="form-group">
                @Html.LabelFor(m => m.Manager.ManagerName, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(m => m.Manager.ManagerName,
                     new SelectList(Enumerable.Empty<SelectListItem>(), "UserId", "ManagerName"), "Select Manager" , new { id = "Manager" })
                    @Html.ValidationMessageFor(m => m.Manager.ManagerName, "", new { @class = "text-danger" })
                </div>
            </div>

****Controller****

[HttpPost]
        public JsonResult FillCity(int depid=2)
        {

          //  var spContext = LocalLeaveAddinWeb.SharePointContextProvider.Current.GetSharePointContext(HttpContext);
            CamlQuery querySelectedDepartment = new CamlQuery();
            querySelectedDepartment.ViewXml = "<Where><Eq><FieldRef Name=\"Department_x003a_ID\" /><Value Type=\"Text\">" + depid + "</Value></Eq></Where>";
            List<Models.Department> Departments = SharePointService.GetSelectedDepManagers(spContext, querySelectedDepartment);

            return Json(**Departments**, JsonRequestBehavior.AllowGet);
        }  <------**HERE I M NOT SURE HOW TO PASS THE Departments VALUE**

Can someone please help me.

1 Answer 1

2

You can use SPServices to Cascade your dropdown lists if they share a lookup relationship.

$().SPServices.SPCascadeDropdowns({
    relationshipList: "ManagerListName",
    relationshipListParentColumn: "Department",    
    relationshipListChildColumn: "Manager",
    parentColumn: "Department", //Internal name of the column
    childColumn: "Manager", //Internal name of the column
    debug: true
  });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.