英文:
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 => x.Status == true && x.CompanyID == CompanyID), "ID", "AccountName");
This is a sample of data now stored in the data table.
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
<div class="col-sm-8">
@Html.DropDownList("CurrentAccountID", null, "--Select Account Type--", htmlAttributes: new { @class = "form-select acctype", @Id = "AccountTypes", @required = true, oninvalid = "this.setCustomValidity('Please Select the Account Type')", oninput = "this.setCustomValidity('')", })
答案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 => 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
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论