在我的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, "--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条答案
按热度按时间46scxncf1#
第一步是在父帐户中包含导航属性:
然后包括父帐户,并按父
AccountName
分组:然后,您必须手动创建select元素以及
option
和optgroup
元素: