如何处理列上的转换
本文关键字:转换 处理 何处理 | 更新日期: 2023-09-27 18:05:04
我为我想要显示的网格的ActionResult设置了这个方法:
public ActionResult ReadTransactions([DataSourceRequest] DataSourceRequest request)
{
var userInfo = new UserInfo(User.Identity);
using (var ctx = new KassenautomatEntities())
{
var transactions = ctx.Protokoll.Where(e => e.KartenID == userInfo.CardId);
var result = transactions.ToDataSourceResult(request);
return Json(result);
}
}
这是我的视图:
@(Html.Kendo().Grid<WebInterface.Models.Protokoll>()
.Name("Transactions")
.Columns(columns =>
{
columns.Bound(o => o.Datum).Format("{0:dd.MM.yyyy HH:mm:ss}");
columns.Bound(o => o.TransaktionTyp);
columns.Bound(o => o.Betrag);
columns.Bound(o => o.Client);
})
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.HtmlAttributes(new { style = "height:430px;" })
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Read(read => read.Action("ReadTransactions", "Transactions"))
)
)
我遇到的问题是这一行视图:
columns.Bound(o => o.TransaktionTyp);
TransaktionTyp
是一个整数,它引用了我想在这里显示的特定字符串。因此,我为它开发了以下enum:
public enum TransactionType
{
Charged = 0,
Debited = 1,
CardBlocked = 2,
CardEstablished = 3,
ProtocolError = 4,
PackageError = 5,
CardCanceled = 6,
CardFree = 7
}
为了解决这个问题,我有以下方法:
public static string TransactionTypeToDisplayName(TransactionType type)
{
switch (type)
{
case TransactionType.Charged:
return "Aufgeladen";
case TransactionType.Debited:
return "Abgebucht";
case TransactionType.CardBlocked:
return "Karte gesperrt";
case TransactionType.CardEstablished:
return "Karte eingerichtet";
case TransactionType.ProtocolError:
return "Protokoll Fehler";
case TransactionType.PackageError:
return "Paket Fehler";
case TransactionType.CardCanceled:
return "Karte Storno";
case TransactionType.CardFree:
return "Karte Gratisabgabe";
default:
return "Unbekannte Transaktionstyp";
}
}
我的问题是,我如何使用我的TransactionTypeToDisplayName
方法格式化我的列?
我想要的是这样的:
columns.Bound(o => TransactionTypeToDisplayName(o.TransaktionTyp))
好吧,这不是我想要的,但这是我最终的解决方案:
首先我写了一个扩展方法:using System;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web.Mvc;
using Kendo.Mvc.UI.Fluent;
namespace WebInterface.Core.Extension
{
/// <summary>
/// Erweiterungsmethoden für das KendoGrid
/// </summary>
public static class KendoGrid
{
/// <summary>
/// Fügt der Tabelle eine Bindung mit einem Enum zu
/// </summary>
/// <typeparam name="TModel">Der Typ des Models</typeparam>
/// <typeparam name="TEnum">Der Typ der Enumeration</typeparam>
/// <param name="model">Das Model</param>
/// <param name="expression">Die Expression</param>
public static void Bound<TModel, TEnum>(this GridColumnFactory<TModel> model, Expression<Func<TModel, byte?>> expression)
where TModel : class
where TEnum : struct, IComparable, IFormattable, IConvertible
{
//Sicherstellen, dass es sich um einen Enum handelt
if (!typeof(TEnum).IsEnum)
{
throw new ArgumentException("T must be an enumerated type");
}
//Die einzelnen Werte des Enums extrahieren
var values = (TEnum[])System.Enum.GetValues(typeof(TEnum));
//Eine Zuweisungsliste von Wert und Anzeigenamen erstellen
var selectList = new SelectList(values.Select(value => new
{
value = Convert.ToInt32(value),
displayName =
value.GetType()
.GetMember(value.ToString(CultureInfo.InvariantCulture))
.First()
.GetCustomAttribute<DisplayAttribute>()
.Name
}), "value", "displayName");
//Fügt die Spalte hinzu
model.ForeignKey(expression, selectList);
}
}
}
第二部分是用Display Attributes重写枚举:
using System.ComponentModel.DataAnnotations;
namespace WebInterface.Core.Enum
{
public class Transaction
{
/// <summary>
/// Die Transaktionstypen
/// </summary>
public enum Type
{
[Display(Name = "Aufgeladen")]
Charged = 0,
[Display(Name = "Abgebucht")]
Debited = 1,
[Display(Name = "Karte Gesperrt")]
CardBlocked = 2,
[Display(Name = "Karte Eingerichtet")]
CardEstablished = 3,
[Display(Name = "Protokoll Fehler")]
ProtocolError = 4,
[Display(Name = "Paket Fehler")]
PackageError = 5,
[Display(Name = "Karte Storno")]
CardCanceled = 6,
[Display(Name = "Karte Gratisabgabe")]
CardFree = 7
}
}
}
要绑定它,我们必须这样调用它:
columns.Bound<Protokoll, Transaction.Type>(o => o.TransaktionTyp);
Protokoll
为实体框架中的模型,Transaction.Type
为枚举。