Open Closed

Create and Edit with required navigational property #9145


User avatar
0
granade created

What's the best way to set the value of a navigational property so the create and edit always have that value. Here's an example. I have a Dealership entity and a Leads entity. Leads has a required navigational property of DealershipId. Here's what I have working.

On the Dealerships razor page, I added and EntityAction.

<EntityAction TItem="DealershipDto" Visible="@CanEditDealership" Clicked="async () => await NavigateToLeads(context)" Text="@L["Leads"]"></EntityAction>

In Dealerships.Extended.razor.cs I added

private Task NavigateToLeads(DealershipDto dealership) { NavigationManager.NavigateTo($"/leads?DealershipId={dealership.Id}"); return Task.CompletedTask; }

Then in LeadsAppService.Extended.cs, I have public async Task\<PagedResultDto> GetListByDealershipAsync(Guid dealershipId, PagedAndSortedResultRequestDto input) { var filterInput = new GetLeadsInput { DealershipId = dealershipId, Sorting = input.Sorting, MaxResultCount = input.MaxResultCount, SkipCount = input.SkipCount };

return await GetListAsync(filterInput); }

This works and gives me a new button to add a lead filtered on the selected dealership.

I tried following the guide in this article but all I could get to work was add buttons not removing or editing.

#1: Is this the best way to do this or is there an obvious "ABP way" that I'm missing?

#2 If it is the best way, how do I disable/hide the default create button (New Lead below)?

#3 On create and edit, how do I hide/disable the Dealership selector as we don't want any chance of it being changed before saving.

The answer to #1 may make questions 2 and 3 moot. Thanks for any help.

John


3 Answer(s)
  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi, here are the answers to your questions:

    #1: Is this the best way to do this or is there an obvious "ABP way" that I'm missing?

    To add a new toolbar item, there is a SetToolbarAsync method in your *.razor.cs file, you can also use it to add a new toolbar. (https://abp.io/docs/latest/framework/ui/blazor/page-header#page-toolbar)

    Example:

            protected virtual ValueTask SetToolbarItemsAsync()
            {
                Toolbar.AddButton(L["ExportToExcel"], async () =>{ await DownloadAsExcelAsync(); }, IconName.Download);
                
                Toolbar.AddButton(L["NewLead"], async () =>
                {
                    await OpenCreateLeadModalAsync();
                }, IconName.Add, requiredPolicyName: Issue9145Permissions.Leads.Create);
    
                return ValueTask.CompletedTask;
            }
    

    #2 If it is the best way, how do I disable/hide the default create button (New Lead below)?

    You can set the order for page toolbar items in the SetToolbarItemsAsync method. (AddButton method has an optional order parameter)

    #3 On create and edit, how do I hide/disable the Dealership selector as we don't want any chance of it being changed before saving.

    While establishing a 1-n relationship in ABP Suite, there is no option to hide the UI part for the selector, I guess this is what you are asking. The best option you have, is setting is as not required and manually hide it in the generated code.


    Regards.

  • User Avatar
    0
    granade created

    Thanks for the suggestions. Is there a way to add my toolbar buttons in the extended.razor.cs so it doesn't get overwritten if we use ABP Suite to make changes later?

    On question #2, I'm not trying to change the order but the visibility. Is there a way to hide or disable the default Create button?

    Thanks,

    John

  • User Avatar
    0
    granade created

    I figured out how to add it from extended file. I missed that I needed a new class that inherits from my entity's base class. What really confused me was in the documentation it provides this article. But this article was the key article as it provided the key info about creating a new class. Based on that articles breadcrumb, I'm not sure it's linked up correctly. I found it by searching and it really helped.

    For anyone else, I'm not sure we can disable toolbar buttons. But once you override and wait for SetToolbarItemsAsync, you can then clear and add back the buttons you need.

    protected override async ValueTask SetToolbarItemsAsync() { Toolbar.Contributors.Clear(); Toolbar.AddButton("Import users from excel", () => { //TODO: Write your custom code return Task.CompletedTask; }, "file-import", Blazorise.Color.Secondary); }

    <br>

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.3.0-preview. Updated on April 16, 2025, 12:13