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
Sachin SinghPosted May 6, 2023, 5:25 PM
use this instead
and on change event
Tuhin PaulPosted May 7, 2023, 3:02 AM
1. Modify the `AddUserModel` class to include the `OnGetRelatedBranches` handler method:
2. Modify the JavaScript code to use the `OnGetRelatedBranches` handler method:
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.
ahmed salahPosted May 6, 2023, 6:14 PM
thanks for support