【发布时间】:2016-09-17 07:21:20
【问题描述】:
在 MVC 应用程序中,有一个 Student 类继承自 ApplicationUser 基类(ASP.NET 标识),其中有一个 ViewModel 称为 StudentViewModel,如下所示:
实体类:
public class ApplicationUser : IdentityUser<int, ApplicationUserLogin,
ApplicationUserRole, ApplicationUserClaim>, IUser<int>
{
public string Name { get; set; }
public string Surname { get; set; }
//code omitted for brevity
}
public class Student: ApplicationUser
{
public int? Number { get; set; }
}
视图模型:
public class StudentViewModel
{
public int Id { get; set; }
public int? Number { get; set; }
//code omitted for brevity
}
我使用以下方法通过在控制器中将StudentViewModel 映射到ApplicationUser 来更新学生:
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult Update([Bind(Exclude = null)] StudentViewModel model)
{
//Mapping StudentViewModel to ApplicationUser ::::::::::::::::
var student = (Object)null;
Mapper.Initialize(cfg =>
{
cfg.CreateMap<StudentViewModel, Student>()
.ForMember(dest => dest.Id, opt => opt.Ignore())
.ForAllOtherMembers(opts => opts.Ignore());
});
Mapper.AssertConfigurationIsValid();
student = Mapper.Map<Student>(model);
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//Then I want to pass the mapped property to the UserManager's Update method:
var result = UserManager.Update(student);
//code omitted for brevity
}
使用这个方法的时候遇到错误:
方法“UserManagerExtensions.Update(UserManager, TUser)”的类型参数无法从用法中推断出来。尝试明确指定类型参数。
有解决办法吗?
【问题讨论】:
-
@BalagurunathanMarimuthu 你有什么想法吗?
标签: c# asp.net-mvc asp.net-identity automapper automapper-5