I have created a dropdown and assigned a api to fetch data into this. This one is working fine during save process. but i could not able to set the saved value in this dropdown since each time the api call is happening during page load. how can i set a selected value in this dropdown based on a branch Id.
class="auto-complete-select"
data-autocomplete-api-url="api/app/branch-settings/branches-from-server/"
data-autocomplete-display-property="name"
data-autocomplete-value-property="id"
data-autocomplete-filter-param-name="name"
data-autocomplete-allow-clear="true">
Vikas SinghPosted May 15, 2024, 1:39 PM
To set a selected value in the dropdown based on a branch ID without triggering another API call during page load, you can use JavaScript to set the selected option after the dropdown has been populated. Here's how you can achieve this:
class="auto-complete-select"
data-autocomplete-api-url="api/app/branch-settings/branches-from-server/"
data-autocomplete-display-property="name"
data-autocomplete-value-property="id"
data-autocomplete-filter-param-name="name"
data-autocomplete-allow-clear="true">
In this code:
@Model.BranchId.This way, the selected value will be set without triggering another API call during page load.
Jayraj ChhayaPosted May 16, 2024, 5:59 AM
To set a selected value in a dropdown in ASP.NET Core based on a branch ID, you can achieve this by modifying the dropdown initialization script. You need to add a JavaScript function that sets the selected value after the dropdown is populated with data from the API.
By retrieving the saved branch ID from the model and setting it as the selected value in the dropdown using jQuery, you can ensure that the dropdown displays the correct value without relying on the API call during page load.
Bineesh ViswanathPosted May 16, 2024, 5:03 AM
@Vikas Singh,
I have tried the code you provided and still not working. One thing i noticed that the api call in cshtml page is triggering only after a click in the dropdown in input area. how can i set this trigger in page load