对于我想要显示的网格,我有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
是一个整数,它引用我想在此处显示的特定字符串。所以我为它开发了以下枚举:
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))
答案 0 :(得分:0)
好吧,不完全是我想要的,但这最终是我的解决方案:
首先我写了一个扩展方法:
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);
}
}
}
第二部分用显示属性重写枚举:
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
是枚举。