I working on MVC razor page with .NET core 7 .I face issue Ajax request not return branches drop down list based on selected value from drop down list country .
with another meaning I need cascade branch drop down list based on country drop down list by using ajax request .
so data display data on branches drop down list will be
select iBranchCode,vBranchDesc from branchs where companyno=companyno (selected value on drop down list company)I try but not give me result
1-create model branches
public class Branch
{
[Key]
public string iBranchCode { get; set; }
public string vBranchDesc { get; set; }
public string CompanyNo { get; set; }
public string CompanyName { get; set; }
}2- on razor page AddUser.cshtml.cs
public class AddUserModel : PageModel
{
[BindProperty]
public UC.ADC.Core.Entities.SQL.User User { get; set; }
private readonly ADCContext _db;
public AddUserModel(ADCContext db)
{
_db = db;
userModel = new AddUserViewModel();
}
public void OnGet()
{
userModel.Branches = _db.Branch.ToList();
userModel.Companies = GetCompanies();
}
public JsonResult GetRelatedBranches(string companyId)
{
var Branches = _db.Branch.Where(p => p.CompanyNo == companyId).ToList();
return new JsonResult(Branches);
}
}3-on html page of AddUser.cshtml
@page "/AddUser"
@model UC.ADC.Host.Pages.Users.AddUserModel
Company
-- Select Company --
@foreach (var company in Model.userModel.Companies)
{
@company.CompanyName
}
Branch
-- Select Branch --
@* @(model.br == branch.Id ? "selected" : "")*@
@foreach (var branch in Model.userModel.Branches)
{
@branch.vBranchDesc
}
@section scripts {
$(document).ready(function () {
$('#company-select').change(function () {
var companyId = $(this).val();
console.log("company id is" + companyId)
if (companyId) {
$.ajax({
url: '@Url.Action("GetRelatedBranches", "AddUserModel")',
type: "GET",
dataType: "json",
data: { companyId: companyId },
success: function (data) {
console.log("data is" + data);
$('#branch-select').empty();
$.each(data, function (i, item) {
$('#branch-select').append($('', {
value: item.iBranchCode,
text: item.vBranchDesc
}));
});
}
});
}
});
});
}The expected result when select company then select branches list related must show based on company no

Sachin SinghRank #66 · 26.2K reputationPosted May 6, 2023, 1:25 PM EDT
use this instead
public JsonResult OnGetRelatedBranches(string companyId) { var Branches = _db.Branch.Where(p => p.CompanyNo == companyId).ToList(); return new JsonResult(Branches); } }and on change event
$.getJSON(`?handler=RelatedBranches&companyId=${companyId}`, (data) => { $.each(data, function (i, item) { $('#branch-select').append($('', { value: item.iBranchCode, text: item.vBranchDesc })); }); });ahmed salahRank #2,240 · 628 reputationPosted May 6, 2023, 2:14 PM EDT
thanks for support
Tuhin PaulRank #7 · 71.9K reputationPosted May 6, 2023, 11:02 PM EDT
1. Modify the `AddUserModel` class to include the `OnGetRelatedBranches` handler method:
public class AddUserModel : PageModel { [BindProperty] public UC.ADC.Core.Entities.SQL.User User { get; set; } private readonly ADCContext _db; public AddUserViewModel userModel { get; set; } public AddUserModel(ADCContext db) { _db = db; userModel = new AddUserViewModel(); } public void OnGet() { userModel.Branches = _db.Branch.ToList(); userModel.Companies = GetCompanies(); } public JsonResult OnGetRelatedBranches(string companyId) { var Branches = _db.Branch.Where(p => p.CompanyNo == companyId).ToList(); return new JsonResult(Branches); } }2. Modify the JavaScript code to use the `OnGetRelatedBranches` handler method:
@section scripts { $(document).ready(function () { $('#company-select').change(function () { var companyId = $(this).val(); console.log("company id is" + companyId) if (companyId) { $.getJSON(`?handler=RelatedBranches&companyId=${companyId}`, (data) => { $('#branch-select').empty(); $.each(data, function (i, item) { $('#branch-select').append($('', { value: item.iBranchCode, text: item.vBranchDesc })); }); }); } }); }); }Note that I changed the URL in the `$.getJSON` call to use the `OnGetRelatedBranches` handler method and modified the success callback to use arrow function syntax.