abp icon indicating copy to clipboard operation
abp copied to clipboard

Cascading dropdown

Open codebycinar opened this issue 6 years ago • 4 comments

Hi everyone, i'm using Abp Version 2.80 but i can upgrade too. I'm using aspnet-core with razor pages.

I'm building a form, which have related entities. Machine Entity

public class Machine : AuditedAggregateRoot<int>, IMultiTenant
    {
        public virtual Guid? TenantId { get; protected set; }
        public string MachineName { get; protected set; }
        public string MachineCode { get; protected set; }
        public int ResourceGroupId { get; protected set; }
        public string Description { get; protected set; }
        public decimal Cost { get; protected set; }
        public int Power { get; protected set; }
        public decimal Capacity { get; protected set; }
        public MachineStatus MachineStatus { get; protected set; }
        public virtual ResourceGroup ResourceGroup { get; set; }
}

ResourceGroup Entity

public class ResourceGroup : AuditedAggregateRoot<int>, IMultiTenant
    {
        public virtual Guid? TenantId { get; protected set; }
        public virtual string ResourceGroupName { get; protected set; }
        [ForeignKey("WorkplaceId")]
        public virtual Workplace Workplace { get; set; }
        public virtual int WorkplaceId { get; protected set; }
        public virtual ICollection<Machine> Machines { get; protected set; }
}

My Form

<form method="post" asp-page="/Operations/CreateModal">
    <abp-modal>
        <abp-modal-header title="@L["NewOperation"].Value"></abp-modal-header>
        <abp-modal-body>
            <abp-select asp-for="SelectedResourceGroup" asp-items="Model.ResourceGroupList"></abp-select>
            <abp-select asp-for="SelectedMachine"></abp-select>
            <abp-input asp-for="Operation.OperationName" />
        </abp-modal-body>
        <abp-modal-footer buttons="@(AbpModalButtons.Cancel|AbpModalButtons.Save)"></abp-modal-footer>
    </abp-modal>
</form>

MachineAppService

public class MachineAppService :
      CrudAppService<Machine, MachineDto, int, PagedAndSortedResultRequestDto,
                     CreateMachineDto, UpdateMachineDto>,
      IMachineAppService
  {
      private readonly IReadOnlyRepository<Machine> _repository;
      public MachineAppService(IRepository<Machine, int> repository)
          : base(repository)
      {
          _repository = repository;
      }

      protected override IQueryable<Machine> CreateFilteredQuery(PagedAndSortedResultRequestDto input)
      {
          var result = _repository.WithDetails(p => p.ResourceGroup);
          return result;
      }
  }

I need to populate Machines dropdown, after user selected a ResourceGroup. How can i achieve this? I think i need to customize my repository - appservice but guides not clear for me.

Thanks.

codebycinar avatar Jul 09 '20 08:07 codebycinar

i have same issue. may be , we have to go out of framework boundary.

mifercagri avatar Jul 09 '20 12:07 mifercagri

You can refer to the module source code to learn custom repository.

https://github.com/abpframework/abp/blob/dev/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs

realLiangshiwei avatar Jul 09 '20 12:07 realLiangshiwei

You can refer to the module source code to learn custom repository.

https://github.com/abpframework/abp/blob/dev/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs

Thanks for reply. But that's not my first concern. I have data already. What i need, how can i use that data? In js file i have this javascript and c# code like below. But nothing happens. I've breakpoint in OnGetMachines method but ajax call not coming here. How can i populate second dropdown?


 $("#SelectedResourceGroup").on("change", function () {
        console.log("Changed");
        var resourceGroup = $(this).val();
        console.log(resourceGroup);
        $("#SelectedMachine").empty();
        $("#SelectedMachine").append("<option value=''>Select Machine</option>");
        $.getJSON(`?handler=Machines&id=${resourceGroup}`, (data) => {
            $.each(data, function (i, item) {
                $("#SelectedMachine").append(`<option value="${item.resourceGroupId}">${item.resourceGroupName}</option>`);
            });
        });
    });
  public void OnGetMachines(int id)
        {
            var machines = _machineAppService.GetMachinesByResourceGroupId(id);
            MachineList = new SelectList(machines, "Id", "MachineName");
        }

codebycinar avatar Jul 09 '20 13:07 codebycinar

It has nothing to do with abp. If you used select2, you should check select2 document: https://select2.org/.

realLiangshiwei avatar Jul 09 '20 13:07 realLiangshiwei