jquery AJAX 请求不返回分支机构下拉列表基于公司下拉列表选择的值?

6yoyoihd  于 2023-05-17  发布在  jQuery
关注(0)|答案(1)|浏览(192)

我工作的MVC剃刀页与。NET核心7。我面临的问题 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-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> 
}

选择公司时的预期结果,然后选择分支机构列表相关必须基于公司编号显示x1c 0d1x

下面的 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> 
}
zzlelutf

zzlelutf1#

我解决了我的问题
该问题存在于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
                                }));
                            });
                        }
                    });
                }
            });

相关问题