如何分组下拉项列表 C#

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

How to group the Drop down Item List C#

问题

在我的asp.net MVC web应用程序中,我创建了一个模型如下,

[Key]
public int ID {
  get;
  set;
}

[Required]
public string AccountName {
  get;
  set;
}

public string Description {
  get;
  set;
}

public int ? CurrentAccountID {
  get;
  set;
}

所以从控制器中,我将数据绑定到ViewBag中,如下,

ViewBag.CurrentAccountID = new SelectList(db.Accounts.Where(x => x.Status == true && x.CompanyID == CompanyID), "ID", "AccountName");

这是现在存储在数据表中的数据的示例。

但是在视图中,我希望在下拉列表中按组显示列表,

收入
  银行
  信用卡
支出
  办公室支出
  其他支出

所以方法是我想按MainAccountID分组并在下拉列表中显示数据。

如果有人能指导我如何做这个,将不胜感激。

这是HTML视图,

<div class="col-sm-8"> 
    @Html.DropDownList("CurrentAccountID", null, "--选择帐户类型--", htmlAttributes: new { @class = "form-select acctype", @Id = "AccountTypes", @required = true, oninvalid = "this.setCustomValidity('请选择帐户类型')", oninput = "this.setCustomValidity('')", })
英文:

In my asp.net MVC web application, I created a model like,

[Key]
public int ID {
  get;
  set;
}

[Required]
public string AccountName {
  get;
  set;
}

public string Description {
  get;
  set;
}

public int ? CurrentAccountID {
  get;
  set;
}

So from the controller, I'm binding the data to the ViewBag like,

ViewBag.CurrentAccountID = new SelectList(db.Accounts.Where(x =&gt; x.Status == true &amp;&amp; x.CompanyID == CompanyID), &quot;ID&quot;, &quot;AccountName&quot;);

This is a sample of data now stored in the data table.

如何分组下拉项列表 C#

But in the view, I want to show the list in the dropdown list grouping by,

Income

Bank

Credit Card

Expenses

Office Expenses

Other Expenses

So the method is I want to group and show MainAccountID-wise data in the dropdown list.

Would appreciate it if anyone guide me on how to do it

This is the html view

&lt;div class=&quot;col-sm-8&quot;&gt; 
    @Html.DropDownList(&quot;CurrentAccountID&quot;, null, &quot;--Select Account Type--&quot;, htmlAttributes: new { @class = &quot;form-select acctype&quot;, @Id = &quot;AccountTypes&quot;, @required = true, oninvalid = &quot;this.setCustomValidity(&#39;Please Select the Account Type&#39;)&quot;, oninput = &quot;this.setCustomValidity(&#39;&#39;)&quot;, })

答案1

得分: 0

以下是您要翻译的代码部分:

First step is to include a navigation property to the parent account:

    public virtual Account ParentAccount { get; set; }

Then include the parent accounts, and group by the parent `AccountName`:

    var accountsGrouped = db.Accounts
        .Include(x => x.ParentAccount) // Include parent accounts
        .Where(x => x.Status == true && x.CompanyID == CompanyID)
        .OrderBy(x => x.ParentAccount.AccountName)
        .GroupBy(x => x.ParentAccount.AccountName) // Group by parent account name
        .ToList();
    
    ViewBag.AccountsGrouped = accountsGrouped;


Then you'll have to manually create the select element and the `option` and [`optgroup`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/optgroup) elements:

    <div class="col-sm-8">
        <select id="AccountTypes" name="CurrentAccountID" class="form-select acctype" required oninvalid="this.setCustomValidity('Please Select the Account Type')" oninput="this.setCustomValidity('')">
            <option value="">--Select Account Type--</option>
            @foreach (var group in ViewBag.AccountsGrouped)
            {
                <optgroup label="@group.Key"> <!-- group.Key is the parent AccountName -->
                    @foreach (var item in group)
                    {
                        <option value="@item.ID">@item.AccountName</option>
                    }
                </optgroup>
            }
        </select>
    </div>
英文:

First step is to include a navigation property to the parent account:

public virtual Account ParentAccount { get; set; }

Then include the parent accounts, and group by the parent AccountName:

var accountsGrouped = db.Accounts
    .Include(x =&gt; x.ParentAccount) // Include parent accounts
    .Where(x =&gt; x.Status == true &amp;&amp; x.CompanyID == CompanyID)
    .OrderBy(x =&gt; x.ParentAccount.AccountName)
    .GroupBy(x =&gt; x.ParentAccount.AccountName) // Group by parent account name
    .ToList();

ViewBag.AccountsGrouped = accountsGrouped;

Then you'll have to manually create the select element and the option and optgroup elements:

&lt;div class=&quot;col-sm-8&quot;&gt;
    &lt;select id=&quot;AccountTypes&quot; name=&quot;CurrentAccountID&quot; class=&quot;form-select acctype&quot; required oninvalid=&quot;this.setCustomValidity(&#39;Please Select the Account Type&#39;)&quot; oninput=&quot;this.setCustomValidity(&#39;&#39;)&quot;&gt;
        &lt;option value=&quot;&quot;&gt;--Select Account Type--&lt;/option&gt;
        @foreach (var group in ViewBag.AccountsGrouped)
        {
            &lt;optgroup label=&quot;@group.Key&quot;&gt; &lt;!-- group.Key is the parent AccountName --&gt;
                @foreach (var item in group)
                {
                    &lt;option value=&quot;@item.ID&quot;&gt;@item.AccountName&lt;/option&gt;
                }
            &lt;/optgroup&gt;
        }
    &lt;/select&gt;
&lt;/div&gt;

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

发表评论

匿名网友

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

确定