级联下拉菜单不触发

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

Cascade Dropdown not trigger

问题

我正在使用asp.net core。我有以下的控制器:

public async Task<IActionResult> Edit(string id, string nomor)
{
    if (id == null || nomor == null || _context.Mfbnkcoms == null)
    {
        return NotFound();
    }

    //var model = await _context.Mfbnkcoms.FindAsync(id);
    var model = await _context.Mfbnkcoms.Where(x => x.MfbnkcomBankId == id && x.MfbnkcomNoRekening == nomor).SingleOrDefaultAsync();
    if (model == null)
    {
        return NotFound();
    }

    var glaccountselect =
        _context.Mfcoas
            .Select(s => new
            {
                AccId = s.MfcoaAccount,
                AccDescription = $"{s.MfcoaAccount} -- &#163;{s.MfcoaDes}"
            })
            .ToList();
    return View(model);
}

public IActionResult GetSubAccount(string sid)
{
    //var SubAccountList = _context.Mfsoas.Where(s => s.MfsoaAcc == sid).Select(c => new { Id = c.MfsoaAccount, Name = c.MfsoaAccount }).ToList();
    var SubAccountList = _context.Mfsoas.Where(s => s.MfsoaAcc == sid).Select(c => new { Id = c.MfsoaAccount, Name = string.Format("{0}-- &#163;{1}", c.MfsoaAccount, c.MfsoaDes) }).ToList();
    return Json(SubAccountList);
}

在视图中:

<div class="form-group row">
    <label for="GLAccount" class="col-md-2 col-form-label">GL Account</label>
    <div class="col-md-6">
        <select asp-for="MfbnkcomAccount" asp-items="ViewBag.Glaccount" class="form-control select2" name="GLAccount" id="GLAccount" data-live-search="true" data-size="11">
            <option value="">-- Select Account --</option>
        </select>
    </div>

    <label for="SubAccount" class="col-md-2 col-form-label">Sub Account</label>
    <div class="col-md-6">
        <select asp-for="MfbnkcomSubAcc" class="form-control select2" name="SubAccount" id="SubAccount" data-live-search="true" data-size="11">
            <option value="">-- Select SubAccount --</option>
        </select>
    </div>
</div>

<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2-bootstrap-theme/0.1.0-beta.10/select2-bootstrap.css" integrity="sha512-CbQfNVBSMAYmnzP3IC+mZZmYMP2HUnVkV4+PwuhpiMUmITtSpS7Prr3fNncV1RBOnWxzz4pYQ5EAGG4ck46Oig==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

<script>
    $('.select2').select2({
        theme: "bootstrap"
    });
</script>

<script>
    $(function () {
        $("select#GLAccount").change(function () {
            var sid = $(this).val();

            $("select#SubAccount").empty();

            $.getJSON(`/BankCompanies/GetSubAccount?sid=${sid}`, function (data) {
                //console.log(data);
                $.each(data, function (i, item) {
                    $("select#SubAccount").append(`<option value="${item.id}">${item.name}</option>`);
                });
            });
        })
    });
</script>

我有一个Index页面,显示数据列表。当我点击表格上的Edit按钮时,它会重定向到Edit页面。编辑页面将显示所选数据的详细信息。

所有数据都显示正常,只有SubAccount选择框中的数据没有显示。为什么会这样?请提供建议。

谢谢。

英文:

I am using asp.net core. I have the following controller:<br>

public async Task&lt;IActionResult&gt; Edit(string id, string nomor)
{
if (id == null || nomor == null || _context.Mfbnkcoms == null)
{
return NotFound();
}
//var model = await _context.Mfbnkcoms.FindAsync(id);
var model = await _context.Mfbnkcoms.Where(x =&gt; x.MfbnkcomBankId == id &amp;&amp; x.MfbnkcomNoRekening == nomor).SingleOrDefaultAsync();
if (model == null)
{
return NotFound();
}
var glaccountselect =
_context.Mfcoas
.Select(s =&gt; new
{
AccId = s.MfcoaAccount,
AccDescription = $&quot;{s.MfcoaAccount} -- &#163;{s.MfcoaDes}&quot;
})
.ToList();
return View(model);
}
public IActionResult GetSubAccount(string sid)
{
//var SubAccountList = _context.Mfsoas.Where(s =&gt; s.MfsoaAcc == sid).Select(c =&gt; new { Id = c.MfsoaAccount, Name = c.MfsoaAccount }).ToList();
var SubAccountList = _context.Mfsoas.Where(s =&gt; s.MfsoaAcc == sid).Select(c =&gt; new { Id = c.MfsoaAccount, Name = string.Format(&quot;{0}-- &#163;{1}&quot;, c.MfsoaAccount, c.MfsoaDes) }).ToList();
return Json(SubAccountList);
}

On the View:<br>

&lt;div class=&quot;form-group row&quot;&gt;
&lt;label for=&quot;GLAccount&quot; class=&quot;col-md-2 col-form-label&quot;&gt;GL Account&lt;/label&gt;
&lt;div class=&quot;col-md-6&quot;&gt;
&lt;select asp-for=&quot;MfbnkcomAccount&quot; asp-items=&quot;ViewBag.Glaccount&quot; class=&quot;form-control select2&quot; name=&quot;GLAccount&quot; id=&quot;GLAccount&quot; data-live-search=&quot;true&quot; data-size=&quot;11&quot;&gt;
&lt;option value=&quot;&quot;&gt;-- Select Account --&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;
&lt;label for=&quot;SubAccount&quot; class=&quot;col-md-2 col-form-label&quot;&gt;Sub Account&lt;/label&gt;
&lt;div class=&quot;col-md-6&quot;&gt;
&lt;select asp-for=&quot;MfbnkcomSubAcc&quot; class=&quot;form-control select2&quot; name=&quot;SubAccount&quot; id=&quot;SubAccount&quot; data-live-search=&quot;true&quot; data-size=&quot;11&quot;&gt;
&lt;option value=&quot;&quot;&gt;-- Select SubAccount --&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;

</div>

&lt;link href=&quot;https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/select2-bootstrap-theme/0.1.0-beta.10/select2-bootstrap.css&quot; integrity=&quot;sha512-CbQfNVBSMAYmnzP3IC+mZZmYMP2HUnVkV4+PwuhpiMUmITtSpS7Prr3fNncV1RBOnWxzz4pYQ5EAGG4ck46Oig==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot; /&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
$(&#39;.select2&#39;).select2({
theme: &quot;bootstrap&quot;
});
&lt;/script&gt;
&lt;script&gt;
$(function () {
$(&quot;select#GLAccount&quot;).change(function () {
var sid = $(this).val();
$(&quot;select#SubAccount&quot;).empty();
$.getJSON(`/BankCompanies/GetSubAccount?sid=${sid}`, function (data) {
//console.log(data);
$.each(data, function (i, item) {
$(&quot;select#SubAccount&quot;).append(`&lt;option value=&quot;${item.id}&quot;&gt;${item.name}&lt;/option&gt;`);
});
});
})
});
&lt;/script&gt;

I have an Index page which is the list of the data. When I click Edit button on the table. It will redirect to Edit page. The edit page will show with the detail of the selected data.<br>

All data appears, only data on SubAccount select is not shown. How this is happen? <br>
Advice please.

Thank you.

答案1

得分: 1

I test your code and set breakpoint at GetSubAccount, but it won't execute the GetSubAccount method. Try to change your code:

$(&quot;select#GLAccount&quot;).on(&quot;change&quot;, function () { 
    var sid = $(this).val();

    $(&quot;select#SubAccount&quot;).empty();

    $.getJSON(`/BankCompanies/GetSubAccount?sid=${sid}`, function (data) {
        //console.log(data);
        $.each(data, function (i, item) {
            $(&quot;select#SubAccount&quot;).append(`&lt;option value=&quot;${item.id}&quot;&gt;${item.name}&lt;/option&gt;`);
        });
    });
})

Update

Add the following code in your edit action:

ViewBag.SubAccountList = _context.Mfsoas
    .Select(s =&gt; new SelectListItem() { Text =  $&quot;{s.MfcoaAccount} -- &#163;{s.MfcoaDes}&quot;, Value = s.MfcoaAccount.ToString()  })
    .ToList();

And add asp-items=&quot;ViewBag.SubAccountList&quot; to your HTML:

<label for=&quot;SubAccount&quot; class=&quot;col-md-2 col-form-label&quot;&gt;Sub Account&lt;/label&gt;
<div class=&quot;col-md-6&quot;&gt;
    <select asp-for=&quot;MfbnkcomSubAcc&quot; asp-items=&quot;ViewBag.SubAccountList&quot; class=&quot;form-control select2&quot; name=&quot;SubAccount&quot; id=&quot;SubAccount&quot; data-live-search=&quot;true&quot; data-size=&quot;11&quot;&gt;
        <option value=&quot;&quot;&gt;-- Select SubAccount --&lt;/option&gt;
    </select&gt;
</div&gt;

Result:

级联下拉菜单不触发

英文:

I test your code and set breakpoint at GetSubAccount ,but it won't excute the GetSubAccount method. Try to change your code :

$(function () {
$(&quot;select#GLAccount&quot;).change(function () {
var sid = $(this).val();
$(&quot;select#SubAccount&quot;).empty();
$.getJSON(`/BankCompanies/GetSubAccount?sid=${sid}`, function (data) {
//console.log(data);
$.each(data, function (i, item) {
$(&quot;select#SubAccount&quot;).append(`&lt;option value=&quot;${item.id}&quot;&gt;${item.name}&lt;/option&gt;`);
});
});
})
});

into( remove $(function () {):

$(&quot;select#GLAccount&quot;).on(&quot;change&quot;, function () { 
// or 
//$(&quot;select#GLAccount&quot;).change(function () {
var sid = $(this).val();
$(&quot;select#SubAccount&quot;).empty();
$.getJSON(`/BankCompanies/GetSubAccount?sid=${sid}`, function (data) {
//console.log(data);
$.each(data, function (i, item) {
$(&quot;select#SubAccount&quot;).append(`&lt;option value=&quot;${item.id}&quot;&gt;${item.name}&lt;/option&gt;`);
});
});
});

result:
级联下拉菜单不触发

Update

Add below in your edit action:

 ViewBag.SubAccountList = _context.Mfsoas
.Select(s =&gt; new SelectListItem() { Text =  $&quot;{s.MfcoaAccount} -- &#163;{s.MfcoaDes}&quot;, Value = s.MfcoaAccount.ToString()  })
.ToList();

And add asp-items=&quot;ViewBag.SubAccountList&quot; :

&lt;label for=&quot;SubAccount&quot; class=&quot;col-md-2 col-form-label&quot;&gt;Sub Account&lt;/label&gt;
&lt;div class=&quot;col-md-6&quot;&gt;
&lt;select asp-for=&quot;MfbnkcomSubAcc&quot; asp-items=&quot;ViewBag.SubAccountList&quot; class=&quot;form-control select2&quot; name=&quot;SubAccount&quot; id=&quot;SubAccount&quot; data-live-search=&quot;true&quot; data-size=&quot;11&quot;&gt;
&lt;option value=&quot;&quot;&gt;-- Select SubAccount --&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年6月19日 15:13:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504375.html
匿名

发表评论

匿名网友

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

确定