Considérez ce qui suitPropriété Kendo Datasource
MODÈLE
public partial class ElementType
{
public long ElementTypeId { get; set; }
public LocalizedString TypeName { get; set; }
}
[ComplexType]
public class LocalizedString
{
public string French { get; set; }
public string English { get; set; }
[NotMapped]
public string Current
{
get { return (string) LanguageProperty().GetValue(this,null); }
set { LanguageProperty().SetValue(this, value,null); }
}
public override string ToString()
{
return Current;
}
private PropertyInfo LanguageProperty()
{
string currentLanguage = Thread.CurrentThread.CurrentUICulture.DisplayName;
return GetType().GetProperty(currentLanguage);
}
}
CONTRÔLEUR
public ActionResult ElementType_Read([DataSourceRequest]DataSourceRequest request)
{
List<ElementType> elementTypeList = db.ElementType.ToList();
IQueryable<ElementType> elementTypes = elementTypeList.AsQueryable();
DataSourceResult result = elementTypes.ToDataSourceResult(request, elementType => new
{
ElementTypeId = elementType.ElementTypeId,
TypeName = elementType.TypeName,
});
return Json(result);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ElementType_Destroy([DataSourceRequest]DataSourceRequest request, ElementType elementType)
{
if (ModelState.IsValid)
{
var currentElementType = db.ElementType.Find(elementType.ElementTypeId);
db.ElementType.Attach(currentElementType);
db.ElementType.Remove(currentElementType);
db.SaveChanges();
}
return Json(new[] { elementType }.ToDataSourceResult(request, ModelState));
}
action Lire fonctionne comme prévu. Je reçois le TypeName courant dans la vue Grille par TypeName.Current
Mais si je tente d'exécuter d'autres actions, comme détruire ou mettre à jour je reçois cette exception Impossible de convertir un objet de type « Iolite .Models.LocalizedString 'pour taper' System.String '.
Une suggestion pour le réparer?
Cordialement
Je ne vois aucune raison d'utiliser un DTO dans ce scénario, spécialement parce que je pourrais avoir besoin de l'objet. De plus, si la propriété 'TypeName' a été définie comme' string', je n'aurais pas 'TypeName.Current' de mon DTO comme indiqué sur votre code. –