Ajax请求未返回根据公司下拉列表选择值而生成的分支下拉列表?

huangapple go评论56阅读模式
英文:

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 =&gt; p.CompanyNo == companyId).ToList();

            return new JsonResult(Branches); 
          
        }
    }

3-on html page of AddUser.cshtml

@page &quot;/AddUser&quot;
@model UC.ADC.Host.Pages.Users.AddUserModel
 &lt;form method=&quot;post&quot;&gt;
        &lt;div class=&quot;form-group row&quot;&gt;
            &lt;label for=&quot;company-select&quot; class=&quot;col-sm-1 col-form-label&quot; style=&quot;font-size:15px;font-family: &#39;Open Sans&#39;, sans-serif;font-weight: bold;&quot;&gt;Company&lt;/label&gt;
            &lt;div class=&quot;col-sm-3&quot;&gt;
                &lt;select id=&quot;company-select&quot; asp-for=&quot;User.CompanyNo&quot; class=&quot;form-control&quot; style=&quot; margin-left:10px;font-size:15px;font-family: &#39;Open Sans&#39; , sans-serif;font-weight: bold;&quot;&gt;
                    &lt;option value=&quot;&quot;&gt;-- Select Company --&lt;/option&gt;
                     @foreach (var company in Model.userModel.Companies)
                     {
       
                        &lt;option value=&quot;@company.CompanyNo&quot;&gt;@company.CompanyName&lt;/option&gt;


                     }
                &lt;/select&gt;
            &lt;/div&gt;
        &lt;/div&gt;

        &lt;div class=&quot;form-group row&quot;&gt;
            &lt;label for=&quot;branch-select&quot; class=&quot;col-sm-1 col-form-label&quot; style=&quot;font-size:15px;font-family: &#39;Open Sans&#39;, sans-serif;font-weight: bold;&quot;&gt;Branch&lt;/label&gt;
            &lt;div class=&quot;col-sm-3&quot;&gt;
                &lt;select id=&quot;branch-select&quot; asp-for=&quot;User.iBranchCode&quot; class=&quot;form-control&quot; style=&quot; margin-left:10px;font-size:15px;font-family: &#39;Open Sans&#39; , sans-serif;font-weight: bold;&quot;&gt;
                    &lt;option value=&quot;&quot;&gt;-- Select Branch --&lt;/option&gt;
                 @*   @(model.br == branch.Id ? &quot;selected&quot; : &quot;&quot;)*@
                     @foreach (var branch in Model.userModel.Branches)
                    {
                        &lt;option value=&quot;@branch.iBranchCode&quot;&gt;@branch.vBranchDesc&lt;/option&gt;
                    }
                &lt;/select&gt;
            &lt;/div&gt;
        &lt;/div&gt;
&lt;/div&gt;
@section scripts {
 &lt;script&gt;
        $(document).ready(function () {
            $(&#39;#company-select&#39;).change(function () {
                var companyId = $(this).val();
                console.log(&quot;company id is&quot; + companyId)
                if (companyId) {
                    $.ajax({
                        url: &#39;@Url.Action(&quot;GetRelatedBranches&quot;, &quot;AddUserModel&quot;)&#39;,
                        type: &quot;GET&quot;,
                        dataType: &quot;json&quot;,
                        data: { companyId: companyId },
                        success: function (data) {
                            console.log(&quot;data  is&quot; + data);
                            $(&#39;#branch-select&#39;).empty();
                            $.each(data, function (i, item) {
                                $(&#39;#branch-select&#39;).append($(&#39;&lt;option&gt;&#39;, {
                                    value: item.iBranchCode,
                                    text: item.vBranchDesc
                                }));
                            });
                        }
                    });
                }
            });
        });
    &lt;/script&gt; 
}

Expected result when select company then select branches list related must show based on company no
Ajax请求未返回根据公司下拉列表选择值而生成的分支下拉列表?

Update post
issue on ajax request below :

@section scripts {
 &lt;script&gt;
        $(document).ready(function () {
            $(&#39;#company-select&#39;).change(function () {
                var companyId = $(this).val();
                console.log(&quot;company id is&quot; + companyId)
                if (companyId) {
                    $.ajax({
                        url: &#39;@Url.Action(&quot;GetRelatedBranches&quot;, &quot;AddUserModel&quot;)&#39;,
                        type: &quot;GET&quot;,
                        dataType: &quot;json&quot;,
                        data: { companyId: companyId },
                        success: function (data) {
                            console.log(&quot;data  is&quot; + data);
                            $(&#39;#branch-select&#39;).empty();
                            $.each(data, function (i, item) {
                                $(&#39;#branch-select&#39;).append($(&#39;&lt;option&gt;&#39;, {
                                    value: item.iBranchCode,
                                    text: item.vBranchDesc
                                }));
                            });
                        }
                    });
                }
            });
        });
    &lt;/script&gt; 
}

答案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: &#39;?handler=RelatedBranches&#39;, and it working fine
I change my ajax request as below

$(document).ready(function () {
            $(&#39;#company-select&#39;).change(function () {
                var companyId = $(this).val();
                console.log(companyId);
                if (companyId) {
                    console.log(companyId);
                    $.ajax({
                      
                        url: &#39;?handler=RelatedBranches&#39;,
                        type: &quot;GET&quot;,
                        dataType: &quot;json&quot;,
                       
                        data: { companyId: companyId },
                        success: function (data) {
                            console.log(data);
                            $(&#39;#branch-select&#39;).empty();
                            $.each(data, function (i, item) {
                      
                                $(&#39;#branch-select&#39;).append($(&#39;&lt;option&gt;&#39;, {
                                    value: item.iBranchCode,
                                    text: item.vBranchDesc
                                }));
                            });
                        }
                    });
                }
            });

huangapple
  • 本文由 发表于 2023年5月6日 21:22:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定