当我尝试将数据提交到数据库时,我收到以下错误:
{"Success":false,"Error":true,"ErrorType":2,"Message":"System.InvalidOperationException: Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.\r\n at AutoMapper.Mapper.get_Instance()\r\n at AutoMapper.Mapper.Map(Object source, Object destination, Type sourceType, Type destinationType)\r\n at GoalPear.Web.Areas.PM.Controllers.CompanyController.SaveData(CompanyFormViewModel companyFormViewModel)
请告诉我为什么会出现这个错误,因为我经过了很多努力都无法弄清楚。
这是我的控制器代码:
public CompanyController() { }
private readonly ICompanyService _companyService;
public CompanyController(ICompanyService companyService) {
_companyService = companyService;
}
这是控制器中的SaveData方法
[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());
}
}
我的表单视图代码
@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>
}
这是我的公司模型代码
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; }
这是 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; }
这是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));
我的viewModeltoDomainMapping配置文件
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));
我为什么会收到“mapper未初始化”的错误?
CreateMap<Company, CompanyFormViewModel>()
并摆脱ForMember
。 - matcartmill