Dropdown list group in C# 中文翻译:C# 中的下拉列表组

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

Dropdown list group in C#

问题

In my ASP.NET MVC web application, I have a table that I planned to design which contains the main account and the subaccounts in the same table.

First I will show how I expected to get the final output on my dropdown list.

Income

Bank Account
Credit Card

Expenses

Office Expenses
Other Expenses

So as you can see upper section, Bold one's are the main accounts and the within that accounts are the sub accounts.

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

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

public int? MainAccountID{ get; set; }

I hope to save the data in the table like this:

Dropdown list group in C# 中文翻译:C# 中的下拉列表组

I want to know, If I saved the data as I show, How to get the data and bind it to the drop-down as I show in the first section, grouping the sub-accounts under main accounts?

This can be done using separate tables, but I thought If I can save it under one table as I expected, it would be easy and fast to get the data from the model and pass it to the view. That's why I want to know about this.

英文:

In my ASP.NET MVC web application, I have a table that I planned to design which contains the main account and the subaccounts in the same table.

First I will show how I expected to get the final output on my dropdown list.

Income

Bank Account
Credit Card

Expenses

Office Expenses
Other Expenses

So as you can see upper section, Bold one's are the main accounts and the within that accounts are the sub accounts.

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

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

public int? MainAccountID{ get; set; }

I hope to save the data in the table like this:

Dropdown list group in C# 中文翻译:C# 中的下拉列表组

I want to know, If I saved the data as I show, How to get the data and bind it to the drop-down as I show in the first section, grouping the sub-accounts under main accounts?

This can be done using separate tables, but I thought If I can save it under one table as I expected, it would be easy and fast to get the data from the model and pass it to the view. That's why I want to know about this.

答案1

得分: 1

Hi, here's the translation of the provided text:

嗯,说实话,我不会采用你的方法,而是建议将这两个模型拆分为两个不同的表格。如果你想要采用两个表格相同并且具有相同结构的方法,可以通过具有相同属性或继承自主表来实现。

我会展示两种方法。

继承

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

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

public class Income : BaseClass {}
    
public class Outcome : BaseClass {}

不继承

public class Income {
   [Key]
   public int ID { get; set; }
    
   [Required]
   public string AccountName { get; set; }
}
        
public class Outcome {
   [Key]
   public int ID { get; set; }
        
   [Required]
   public string AccountName { get; set; }
}

个人而言,我不会使用继承,因为这些表格非常简单。

要创建选择列表,你可以使用以下代码:

new SelectList(_db.Incomes.OrderBy(x => x.AccountName), "ID", "AccountName");

希望这对解决你的问题有所帮助。

如果你确实想将所有行放在一起并做一些稍微疯狂的事情(依我个人看法),那么你可以这样做:

public class Account {
   public int ID { get; set; } 
    
   [Required]
   public string AccountName { get; set; }
    
   public int? MainAccountID { get; set; }
    
   [ForeignKey("MainAccountID")]
   public virtual MainAccount MainAccount { get; set; }
}

然后使用以下代码:

var d = _db.Accounts.Where(x => x.MainAccountID != null)
                    .Include(x => x.MainAccount)
                    .OrderBy(x => x.MainAccount.AccountName)
                    .ThenBy(x => x.AccountName);
    
return new SelectList(d, "MainAccountID", "AccountName", "MainAccount.AccountName", null, null);

这将按照收入/支出行进行选择分组。

英文:

Hi so TBH I wouldn't do your approach and would advocate splitting up the 2 Models into 2 different tables. You could if you want to take the apprach of have both tables being the same and using the same structure either by just having the same attributes or inheriting from the primary.

I'll show you both.

Inheriting

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

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

public class Income : BaseClass  {}

public class Outcome: BaseClass  {}

Non Inheriting

public class Income {
       [Key]
       public int ID { get; set; }
    
       [Required]
       public string AccountName { get; set; }
}
    
    public class Outcome {
      [Key]
       public int ID { get; set; }
    
       [Required]
       public string AccountName { get; set; }

   }

Personnally I wouldn't do Inheriting as the tables are really simple.

To make a select list you can use

new SelectList(_db.Incomes.OrderBy(x => x.AccountName), "ID", "AccountName");

I hope that helps with your problem and sorts you out.

If you did want to have all the rows together and do something a tad crazy (IMHO) then you could do this.

public class Account {
   
   public int ID {get;set;} 

     [Required]
           public string AccountName { get; set; }
  
    public int? MainAccountID{ get; set; }

    [ForeignKey("MainAccountID")]
    public virtual MainAccount{ get; set; }

}

Then use

var d = _db.Accounts.Where(x => x.MainAccountID != null).Include(x => x.MainAccount).OrderBy(x => x.MainAccount.AccountName).ThenBy(x => x.AccountName);

 return new SelectList(d, "MainAccountID", "AccountName ", "MainAccount.AccountName ", null, null);

Which would give a select group by the Incoming/Expenses rows.

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

发表评论

匿名网友

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

确定