Modèle d'entité
public partial class Categoies
{
public Categoies()
{
this.Posts = new HashSet<Posts>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Nullable<int> PositionId { get; set; }
public virtual CategoryPositions CategoryPositions { get; set; }
public virtual ICollection<Posts> Posts { get; set; }
}
Voir le modèle
public class CategoriesViewModel
{
public int Id { get; set; }
[Required(ErrorMessage = "{0} alan bo braklmamaldr!")]
[Display(Name = "Kategori Ad")]
public string Name { get; set; }
[Display(Name = "Kategori Açklama")]
public string Description { get; set; }
[Display(Name = "Kategori Pozisyon")]
[Required(ErrorMessage="{0} alan bo braklmamaldr!")]
public int PositionId { get; set; }
}
CreateMap
Mapper.CreateMap<CategoriesViewModel, Categoies>()
.ForMember(c => c.CategoryPositions, option => option.Ignore())
.ForMember(c => c.Posts, option => option.Ignore());
Carte
[HttpPost]
public ActionResult _EditCategory(CategoriesViewModel viewModel)
{
using (NewsCMSEntities entity = new NewsCMSEntities())
{
if (ModelState.IsValid)
{
try
{
category = entity.Categoies.Find(viewModel.Id);
AutoMapper.Mapper.Map<CategoriesViewModel, Categoies>(viewModel, category);
//category = AutoMapper.Mapper.Map<CategoriesViewModel, Categoies>(viewModel);
//AutoMapper.Mapper.Map(viewModel, category);
entity.SaveChanges();
// Veritaban ilemleri baarl ise yönlendirilecek sayfay
// belirleyip ajax-post-success fonksiyonuna gönder.
return Json(new { url = Url.Action("Index") });
}
catch (Exception ex)
{
}
}
// Veritaban ilemleri baarsz ise modeli tekrar gönder.
ViewBag.Positions = new SelectList(entity.CategoryPositions.ToList(), "Id", "Name");
return PartialView(viewModel);
}
}
Error
Configuration de la carte de type manquante ou mappage non pris en charge. Mappage des types : CategoriesViewModel -> Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D NewsCMS.Areas.Admin.Models.CategoriesViewModel -> System.Data.Entity.DynamicProxies.Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D
Chemin de destination : Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D
Valeur source : NewsCMS.Areas.Admin.Models.CategoriesViewModel
Qu'est-ce que je rate ? J'essaie de trouver, mais je ne vois pas le problème.
UPDATE
J'ai spécifié dans application_start dans Global.asax
protected void Application_Start()
{
InitializeAutoMapper.Initialize();
}
InitializeClass
public static class InitializeAutoMapper
{
public static void Initialize()
{
CreateModelsToViewModels();
CreateViewModelsToModels();
}
private static void CreateModelsToViewModels()
{
Mapper.CreateMap<Categoies, CategoriesViewModel>();
}
private static void CreateViewModelsToModels()
{
Mapper.CreateMap<CategoriesViewModel, Categoies>()
.ForMember(c => c.CategoryPositions, option => option.Ignore())
.ForMember(c => c.Posts, option => option.Ignore());
}
}