我使用 .NET core 7 在 MVC razor 页面上工作。我面临 Ajax 请求不返回分支下拉列表基于从下拉列表公司中选择的值的问题。
另一个意思是我需要使用ajax请求基于公司下拉列表的级联分支下拉列表。
所以分支下拉列表中的数据显示数据将是
select iBranchCode,vBranchDesc from branchs where companyno=companyno (selected value on drop down list company)
我尝试但没有给我结果
1-创建模型分支
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-在剃须刀页面上
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
AddUser.cshtml
的html页面
@page "/AddUser"
@model UC.ADC.Host.Pages.Users.AddUserModel
<form method="post">
<div class="form-group row">
<label for="company-select" class="col-sm-1 col-form-label" style="font-size:15px;font-family: 'Open Sans', sans-serif;font-weight: bold;">Company</label>
<div class="col-sm-3">
<select id="company-select" asp-for="User.CompanyNo" class="form-control" style=" margin-left:10px;font-size:15px;font-family: 'Open Sans' , sans-serif;font-weight: bold;">
<option value="">-- Select Company --</option>
@foreach (var company in Model.userModel.Companies)
{
<option value="@company.CompanyNo">@company.CompanyName</option>
}
</select>
</div>
</div>
<div class="form-group row">
<label for="branch-select" class="col-sm-1 col-form-label" style="font-size:15px;font-family: 'Open Sans', sans-serif;font-weight: bold;">Branch</label>
<div class="col-sm-3">
<select id="branch-select" asp-for="User.iBranchCode" class="form-control" style=" margin-left:10px;font-size:15px;font-family: 'Open Sans' , sans-serif;font-weight: bold;">
<option value="">-- Select Branch --</option>
@* @(model.br == branch.Id ? "selected" : "")*@
@foreach (var branch in Model.userModel.Branches)
{
<option value="@branch.iBranchCode">@branch.vBranchDesc</option>
}
</select>
</div>
</div>
</div>
@section scripts {
<script>
$(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($('<option>', {
value: item.iBranchCode,
text: item.vBranchDesc
}));
});
}
});
}
});
});
</script>
}
选择公司然后选择分支机构列表时的预期结果必须根据公司编号显示
更新帖子 关于下面的 ajax 请求的问题:
@section scripts {
<script>
$(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($('<option>', {
value: item.iBranchCode,
text: item.vBranchDesc
}));
});
}
});
}
});
});
</script>
}
我解决了我的问题
问题存在于 URL ajax 请求不正确所以 ajax
请求不工作。
更改 URL ajax 请求后它工作完美
我将 URL ajax 请求更改为
url: '?handler=RelatedBranches',
并且工作正常
我改变我的 ajax 请求如下
$(document).ready(function () {
$('#company-select').change(function () {
var companyId = $(this).val();
console.log(companyId);
if (companyId) {
console.log(companyId);
$.ajax({
url: '?handler=RelatedBranches',
type: "GET",
dataType: "json",
data: { companyId: companyId },
success: function (data) {
console.log(data);
$('#branch-select').empty();
$.each(data, function (i, item) {
$('#branch-select').append($('<option>', {
value: item.iBranchCode,
text: item.vBranchDesc
}));
});
}
});
}
});