Lorsque j'essaie de soumettre des données dans la base de données, j'obtiens l'erreur suivante:
{"Success": false, "Error": true, "ErrorType": 2, "Message": "System.InvalidOperationException: Mapper non initialisé. Appelez Initialize avec la configuration appropriée. Si vous essayez d'utiliser des instances de mappeur via un conteneur ou sinon, assurez-vous que vous n'avez aucun appel aux méthodes statiques Mapper.Map, et si vous\u0027re utilisez les méthodes d'extension ProjectTo ou UseAsDataSource, assurez-vous de transmettre l'instance IConfigurationProvider appropriée.\r\n à AutoMapper.Mapper.get_Instance ()\r\n sur AutoMapper.Mapper.Map (source d'objet, destination d'objet, type sourceType, type destinationType)\r\n sur GoalPear.Web.Areas.PM.Controllers.CompanyController.SaveData (CompanyFormViewModel companyFormViewModel)
Veuillez me dire pourquoi j'obtiens cette erreur car je ne suis pas en mesure de le comprendre après tant d'efforts.
Voici mon code contrôleur
public CompanyController() { }
private readonly ICompanyService _companyService;
public CompanyController(ICompanyService companyService) {
_companyService = companyService;
}
Ceci est la méthode SaveData dans le contrôleur
[HttpPost]
public JsonResult SaveData(CompanyFormViewModel companyFormViewModel)
{
try
{
int? id = null;
Company company;
if (companyFormViewModel.Id == null)
{
company = new Company();
}
else
{
id = (int)companyFormViewModel.Id;
company = _companyService.GetCompany((int)id);
}
company = (Company)Mapper.Map(
companyFormViewModel,
company,
typeof(CompanyFormViewModel),
typeof(Company));
CompanyValidator companyValidator = new CompanyValidator();
ValidationResult validationResult = companyValidator.Validate(company);
if (validationResult.IsValid) //check for any validation errors
{
if (id == null)
{
_companyService.CreateCompany(company);
}
else
{
_companyService.Update(company);
}
_companyService.SaveCompany();
return new JsonSuccessResult();
}
else
{
Response.StatusCode = (int)ResponseCode.UnprocessableEntity;
return new JsonErrorResult(validationResult);
}
}
catch (Exception ex)
{
Response.StatusCode = (int)ResponseCode.UnprocessableEntity;
return new JsonErrorResult(ex.ToString());
}
}
Code d'affichage de mon formulaire
@using (Html.BeginForm("SaveData", "Company", FormMethod.Post, new { @class = "", @id = "validate", @role = "form" })) {
@*<form id="validate" method="POST" action="javascript:alert('Form #validate submited');">*@
<div class="head tac">
<h1>Register Your Company With Us!!!</h1>
</div>
<div class="block">
<div class="header">
<div class="side pull-right">
<button class="btn btn-default btn-clean" onclick="clear_form('#validate');" type="button">Clear form</button>
</div>
</div>
<div class="content controls">
<div class="form-row">
<div class="col-md-3">Company Name:</div>
<div class="col-md-9">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "validate[required,maxSize[100]]", @placeholder = @Html.DisplayNameFor(model => model.Name) } })
</div>
</div>
<div class="form-row">
<div class="col-md-3">Owner Name:</div>
<div class="col-md-9">
@Html.EditorFor(model => model.Owner, new { htmlAttributes = new { @class = "validate[required,maxSize[60]]", @placeholder = @Html.DisplayNameFor(model => model.Owner) } })
</div>
</div>
<div class="form-row">
<div class="col-md-3">Email:</div>
<div class="col-md-4">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "validate[required,maxSize[60]]" ,@type="email", @placeholder = @Html.DisplayNameFor(model => model.Email) } })
</div>
</div>
<div class="form-row">
<div class="col-md-3">Password:</div>
<div class="col-md-9">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "validate[required,maxSize[100]]", @type="password", @id="password", @placeholder = @Html.DisplayNameFor(model => model.Password) } })
</div>
</div>
<div class="form-row">
<div class="col-md-3">Confirm Password:</div>
<div class="col-md-9">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "validate[required,equals[password]]", @type = "password", @placeholder = @Html.DisplayNameFor(model => model.Password) } })
</div>
</div>
@* I have omitted some code from here for saving your time to read*@
<div class="form-row">
<div class="col-md-3">Address:</div>
<div class="col-md-9">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "validate[required,maxSize[60]]", @placeholder = @Html.DisplayNameFor(model => model.Address) } })
</div>
</div>
</div>
<div class="footer">
<div class="side pull-right">
<div class="btn-group">
<button class="btn btn-default" type="button" onclick="$('#validate').validationEngine('hide');">Hide prompts</button>
<button class="btn btn-success" type="submit">Submit</button>
</div>
</div>
</div>
</div>
}
Voici mon code de modèle d'entreprise
public string Name {get;set;}
public string Logo { get; set; }
public string Address { get; set; }
public string Owner { get; set; }
public int Size { get; set; }
public string Email { get; set; }
public int Phone { get; set; }
public int FaxNo { get; set; }
public string Password { get; set; }
public string Country { get; set; }
public string State { get; set; }
Il s'agit du code ViewModel
public CompanyFormViewModel() { }
public int? Id { get; set; }
[DisplayName(" Company Name")]
public string Name { get; set; }
[DisplayName("Owner")]
public string Owner { get; set; }
[DisplayName("Email")]
public string Email { get; set; }
[DisplayName("Password")]
public string Password { get; set; }
[DisplayName("Address")]
public string Address { get; set; }
Voici le profil DomainToViewModelMapping
cfg.CreateMap<Company, CompanyFormViewModel>()
.ForMember(vm => vm.Id, map => map.MapFrom(m => m.Id))
.ForMember(vm => vm.Name, map => map.MapFrom(m => m.Name))
.ForMember(vm => vm.Owner, map => map.MapFrom(m => m.Owner))
.ForMember(vm => vm.Email, map => map.MapFrom(m => m.Email))
.ForMember(vm => vm.Password, map => map.MapFrom(m => m.Password))
.ForMember(vm => vm.Address, map => map.MapFrom(m => m.Address));
Mon profilModeltoDomainMapping Profile
cfg.CreateMap<CompanyFormViewModel, Company>()
.ForMember(m => m.Id, map => map.MapFrom(vm => vm.Id))
.ForMember(m => m.Name, map => map.MapFrom(vm => vm.Name))
.ForMember(m => m.Owner, map => map.MapFrom(vm => vm.Owner))
.ForMember(m => m.Email, map => map.MapFrom(vm => vm.Email))
.ForMember(m => m.Password, map => map.MapFrom(vm => vm.Password))
.ForMember(m => m.Address, map => map.MapFrom(vm => vm.Address));
Pourquoi ai-je cette erreur de mappeur non initialisée?
Impossible de voir où vous initialisez le mappage.
Solution:
Vous pouvez créer votre profil de mappage, par exemple:
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Company, CompanyFormViewModel>()
.ForMember(vm => vm.Id, map => map.MapFrom(m => m.Id))
.ForMember(vm => vm.Name, map => map.MapFrom(m => m.Name))
.ForMember(vm => vm.Owner, map => map.MapFrom(m => m.Owner))
.ForMember(vm => vm.Email, map => map.MapFrom(m => m.Email))
.ForMember(vm => vm.Password, map => map.MapFrom(m => m.Password))
.ForMember(vm => vm.Address, map => map.MapFrom(m => m.Address));
CreateMap<CompanyFormViewModel, Company>()
.ForMember(m => m.Id, map => map.MapFrom(vm => vm.Id))
.ForMember(m => m.Name, map => map.MapFrom(vm => vm.Name))
.ForMember(m => m.Owner, map => map.MapFrom(vm => vm.Owner))
.ForMember(m => m.Email, map => map.MapFrom(vm => vm.Email))
.ForMember(m => m.Password, map => map.MapFrom(vm => vm.Password))
.ForMember(m => m.Address, map => map.MapFrom(vm => vm.Address));
}
}
Et puis initialiser dans le profil de mappage dans Global.asax.cs
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<MappingProfile>();
});
Mapper.Configuration.AssertConfigurationIsValid();
}
}
Enfin, n'oubliez pas de configurer le mappeur automatique dans votre Application_Start()
//Configure Automapper
AutoMapperConfiguration.Configure();
Au lieu d'utiliser la méthode statique Mapper.Map
, Vous devez utiliser une instance de mappeur créée via la configuration, comme suit:
var mapper = cfg.CreateMapper();
var company = (Company)mapper.Map(companyFormViewModel, company, typeof(CompanyFormViewModel), typeof(Company));
La manière préférée de travailler avec mapper dans les dernières versions est via l'interface IMapper
. Si vous utilisez également le conteneur DI, vous pouvez lier/enregistrer IMapper
pour résoudre l'instance via la surcharge de méthode config.CreateMapper()
qui prend le délégué d'usine d'instance (c'est-à-dire la méthode de fabrique d'instance de conteneur). Si vous n'utilisez pas le conteneur DI, vous pouvez créer par exemple la classe MapperFactory
qui sera responsable de la création de l'instance de mappeur et de sa configuration.
Exemple:
var config = new MapperConfiguration(cfg =>
{
//cfg.CreateMap()...
//cfg.AddProfile()... etc...
});
var mapper = config.CreateMapper();