英文:
Ajax request not return branches drop down List based on company drop down list selected value?
问题
I'm here to assist with your translation request. Here's the translated content:
我正在使用.NET Core 7开发MVC Razor页面。我遇到了一个问题,Ajax请求未返回基于下拉列表公司选择的值的分支下拉列表。
换句话说,我需要使用Ajax请求基于公司下拉列表级联分支下拉列表。
因此,分支下拉列表上显示的数据将是:
select iBranchCode,vBranchDesc from branchs where companyno=companyno(从公司下拉列表中选择的值)
我尝试了但没有得到结果。
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- Razor页面中的 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- 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;">公司</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="">-- 选择公司 --</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;">分支</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="">-- 选择分支 --</option>
@foreach (var branch in Model.userModel.Branches)
{
<option value="@branch.iBranchCode">@branch.vBranchDesc</option>
}
</select>
</div>
</div>
</form>
@section scripts {
<script>
$(document).ready(function () {
$('#company-select').change(function () {
var companyId = $(this).val();
console.log("公司ID是" + companyId)
if (companyId) {
$.ajax({
url: '@Url.Action("GetRelatedBranches", "AddUserModel")',
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
}));
});
}
});
}
});
});
</script>
}
英文:
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 company .
with another meaning I need cascade branch drop down list based on company 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
<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>
}
Expected result when select company then select branches list related must show based on company no
Update post
issue on ajax request below :
@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>
}
答案1
得分: 1
I solved my issue.
问题出在URL的Ajax请求不正确,所以Ajax请求不起作用。
在更改URL的Ajax请求后,它正常工作了。
我将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
}));
});
}
});
}
});
});
英文:
I solved my issue
the issue exist on URL ajax request not correct so ajax
request not working .
after change URL ajax request it working perfect
I change URL ajax request to url: '?handler=RelatedBranches',
and it working fine
I change my ajax request as below
$(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
}));
});
}
});
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论