ASP.Net MVC使用Enum字段EF6中的dropdownfield过滤数据

时间:2022-09-18 08:28:11

I using ASP.Net 4.5 MVC EF6 Entity Framework

我使用ASP.Net 4.5 MVC EF6实体框架

I have an Enum in my model. I like to filter / sort the list by where clause on enum type field.To do this I build a DroppDownField. My SelectList:

我的模型中有一个Enum。我喜欢通过枚举类型字段上的where子句对列表进行过滤/排序。为此,我构建了一个DroppDownField。我的选择列表:

 var  FGenre = Enum.GetValues(typeof(FileGenre)).Cast<FileGenre>().Select(c => new SelectListItem{ Text = c.ToString(), Value=  c.ToString() });

so I get a list (value and text are the text in the Enum).

所以我得到一个列表(值和文本是枚举中的文本)。

Now I like to filter my data with 'Where' like:

现在我想用'Where'过滤我的数据,如:

var model =  db.Filetypes.Where(fg => fg.FileGenres.Equals(????????).);
  1. How can I buid a where claus to filter by Enum

    我怎样才能通过枚举来匹配where claus进行过滤

    or

  2. How can I get a List for the DropDownList with Value = Value(int) and Text, and how can I filter with the Enum( Int).
  3. 如何使用Value = Value(int)和Text获取DropDownList的List,以及如何使用Enum(Int)进行过滤。

I'm still new to LINQ, Lambda and MVC in general. If someone has a way to better this code, please feel free to add in your views.

我一般都是LINQ,Lambda和MVC的新手。如果有人有办法改进此代码,请随时添加您的观点。

Edit: My Model:

编辑:我的模特:

   public partial class Filetype
{

    [Display(Name = "ID")]
    public int FiletypeId { get; set; }

    [StringLength(128)]
    [Display(Name="Datei Art")]
    public string TypeName { get; set; }


    [Display(Name = "Genre")]
    public FileGenre FileGenres { get; set; }

    .......

    public virtual ICollection<FiletypeRole> FiletypeRoles { get; set; }
}
public enum FileGenre
{
    NNB_A,
    NNB_B,
    NNB_C
}  

1 个解决方案

#1


0  

The problem is that the selectlist is representing the enum as a string, so you'd have to convert it to an enum in the query, which is rather ugly:

问题是selectlist将枚举表示为字符串,因此您必须将其转换为查询中的枚举,这相当丑陋:

FileGenre MyGenre = (FileGenre) Enum.Parse(typeof(FileGenre), model.FileGenre, true);

So instead, you can use the enum with int values. Declare it like this:

因此,您可以将枚举与int值一起使用。声明如下:

public enum FileGenre
{
    NNB_A = 1,
    NNB_B = 2,
    NNB_C = 3
}  

And to populate the selectlist (Here I have created a method to return the selectlist):

并填充选择列表(这里我创建了一个返回选择列表的方法):

public SelectList GetAsSelectList()
{
    var enumList = (from Enum e in Enum.GetValues(typeof(FileGenre))
                   select new 
                   {
                       Name = e,
                       Id = Convert.ToInt32(e).ToString()
                   }).ToList();
   return new SelectList((IEnumerable)enumList, "Id", "Name");
}

Now the selectlist will have an int value and will bind correctly when you POST it and use it in the filtering:

现在,selectlist将具有一个int值,并在POST后正确绑定并在过滤中使用它:

var model = db.Filetypes.Where(fg => fg.FileGenres == model.FileGenre).ToList();

As @Alberto mentioned, the FileGenre property on Filetype should be singular, as FileGenres make it seem like a collection or list, which could be confusing.

正如@Alberto所提到的,Filetype上的FileGenre属性应该是单数的,因为FileGenres使它看起来像一个集合或列表,这可能会令人困惑。

#1


0  

The problem is that the selectlist is representing the enum as a string, so you'd have to convert it to an enum in the query, which is rather ugly:

问题是selectlist将枚举表示为字符串,因此您必须将其转换为查询中的枚举,这相当丑陋:

FileGenre MyGenre = (FileGenre) Enum.Parse(typeof(FileGenre), model.FileGenre, true);

So instead, you can use the enum with int values. Declare it like this:

因此,您可以将枚举与int值一起使用。声明如下:

public enum FileGenre
{
    NNB_A = 1,
    NNB_B = 2,
    NNB_C = 3
}  

And to populate the selectlist (Here I have created a method to return the selectlist):

并填充选择列表(这里我创建了一个返回选择列表的方法):

public SelectList GetAsSelectList()
{
    var enumList = (from Enum e in Enum.GetValues(typeof(FileGenre))
                   select new 
                   {
                       Name = e,
                       Id = Convert.ToInt32(e).ToString()
                   }).ToList();
   return new SelectList((IEnumerable)enumList, "Id", "Name");
}

Now the selectlist will have an int value and will bind correctly when you POST it and use it in the filtering:

现在,selectlist将具有一个int值,并在POST后正确绑定并在过滤中使用它:

var model = db.Filetypes.Where(fg => fg.FileGenres == model.FileGenre).ToList();

As @Alberto mentioned, the FileGenre property on Filetype should be singular, as FileGenres make it seem like a collection or list, which could be confusing.

正如@Alberto所提到的,Filetype上的FileGenre属性应该是单数的,因为FileGenres使它看起来像一个集合或列表,这可能会令人困惑。