How to display description or enumeration name in grid line?

I am using Kendo mesh in my ASP.Net MVC application. If I have the following grid definition,

@(Html.Kendo().Grid(Model) //Bind the grid to ViewBag.Products .Name("grid") .Columns(columns => { columns.Bound(p => p.FullName); columns.Bound(p => p.MyEnum) }) .Groupable() .Pageable() .Sortable() .Scrollable(scr => scr.Height(600)) .Filterable() ) 

where one of the columns is Enum. My listing definition:

 public enum MyEnum { [Display(AutoGenerateField = false, Name = "My enum 1", Description = "My Enum 1")] EnumOne, [Display(Name = "My Enum 2")] EnumTwo } 

How do I display "My Enum 1" or "My Enum 2" for each line?

Thanks in advance!

+6
source share
4 answers

I recently ran into this problem and solved it using

 var someArrayOfValueAndText = new[] {new { Id = 0, Description = "Foo" }, new { Id = 1, Description = "Bar" } .Columns(c.ForeignKey(m=> m.MyEnum, someArrayOfValueAndText, "Id","Description")) 

instead of .Bound method

+6
source

I created a helper class containing some extension methods while back while:

 public static class EnumExtensions { public static string GetDisplayName(this Enum enu) { var attr = GetDisplayAttribute(enu); return attr != null ? attr.Name : enu.ToString(); } public static string GetDescription(this Enum enu) { var attr = GetDisplayAttribute(enu); return attr != null ? attr.Description : enu.ToString(); } private static DisplayAttribute GetDisplayAttribute(object value) { Type type = value.GetType(); if (!type.IsEnum) { throw new ArgumentException(string.Format("Type {0} is not an enum", type)); } // Get the enum field. var field = type.GetField(value.ToString()); return field == null ? null : field.GetCustomAttribute<DisplayAttribute>(); } } 

It contains two methods for retrieving the Name and Description attributes of the Display attribute. Display Name:

 columns.Bound(p => p.MyEnum.GetDisplayName()) 

And for the description:

 columns.Bound(p => p.MyEnum.GetDescription()) 

You need to add a using statement in Web.config or in your view.

Update

What to do if you create a property for it in your model:

 public string MyEnumName { get { return MyEnum.GetDisplayName(); } } 

Now you can use:

 columns.Bound(p => p.MyEnumName); 
+2
source

Henk's decision is good. But you can use filtering if you use ClientTemplate:

 col.Bound(m => m.MyEnum) // this provides you filtering .ClientTemplate("#: MyEnumName #") // this shows a name of enum 

For more information about kendo ui templates, see http://docs.telerik.com/kendo-ui/framework/templates/overview

+2
source

I am using the @ user1967246 method and would like to explain how to do this.

i created an array at the top of my kendo grid

  var statusLikeEntityStatus = new[] { new {Id = 0, Status = EntityStatus.Passive}, new {Id = 1, Status = EntityStatus.Active}, new {Id = 2, Status = EntityStatus.Draft}, new {Id = 3, Status = EntityStatus.ReadyToLive}, new {Id = -1, Status = EntityStatus.Freezed}, new {Id = -2, Status = EntityStatus.Blocked} }; 

Then I use the ForeignKey property instead of Bound.

  columns.ForeignKey(m => m.Status, statusLikeEntityStatus, "Id", "Status").Title(Resources.General.Status); 

Here is my column attribute

 .Columns(columns => { columns.Bound(m => m.InventoryID).Title("Id"); columns.Bound(m => m.ERPCode).Title(Resources.Products.ProductCode); columns.Bound(m => m.Price).Title(Resources.Products.ListPrice); columns.Bound(m => m.Discount).Title(Resources.Products. columns.Bound(m => m.Stock).Title(Resources.Products.TotalStock); // todo: Resources columns.ForeignKey(m => m.Status, statusLikeEntityStatus, "Id", "Status").Title(Resources.General.Status); columns.Command(commandConf => { commandConf.Edit(); commandConf.Destroy(); }); }) 

Hope this helps you.

+2
source

Source: https://habr.com/ru/post/956790/


All Articles