从 MVC 操作以 JSON 形式返回带有 Children 的 EF 实体
我有一个使用 EF 的 MVC 3 应用程序。在我的用户登录中,我使用 ajax 对用户进行身份验证,并且我想返回需要他们选择的内容的列表。我有 ViewModelLogin ,它有一个 IEnumerable 。我使用 AutoMapper 从 EF 实体填充 ViewModel,然后以 JSON 形式返回 ViewModel。
[HttpPost]
public ActionResult Login(ViewModelLogin login)
{
Mapper.CreateMap<ViewModelLogin, User>()
.ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.Email))
.ForMember(dest => dest.Password, opt => opt.MapFrom(src => src.Password));
var user = _userService.GetUser(Mapper.Map<ViewModelLogin, User>(login));
if (user != null && CreatePasswordHash(login.Password, user.Salt) == user.Password)
{
var ticket = new FormsAuthenticationTicket(1, login.Email, DateTime.Now, DateTime.Now.AddMinutes(20), false, "");
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
Response.Cookies.Add(cookie);
Mapper.CreateMap<User, ViewModelLogin>()
.ForMember(dest => dest.Things, opt => opt.MapFrom(src => src.Things))
.ForMember(dest => dest.Password, opt => opt.Ignore());
var returnLogin = Mapper.Map<User, ViewModelLogin>(user);
returnLogin.Success = true;
return Json(returnLogin);
}
return Json(new ViewModelLogin { Success = false, Message = "Test Message"});
}
当我这样做时,当序列化程序尝试将实体转换为 JSON 时,我会收到错误。问题是循环依赖,因为集合保留了来自 EF 的关系信息(在我的例子中是多对多关系),除了手动执行之外,还有其他人解决了这个问题吗?
谢谢
I have an MVC 3 app using EF. On my user login I authenticate the user using ajax and I want to return a list of things that I need them to select. I have ViewModelLogin that has a IEnumerable. I am using AutoMapper to populate the ViewModel from the EF Entity and then return the ViewModel as JSON.
[HttpPost]
public ActionResult Login(ViewModelLogin login)
{
Mapper.CreateMap<ViewModelLogin, User>()
.ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.Email))
.ForMember(dest => dest.Password, opt => opt.MapFrom(src => src.Password));
var user = _userService.GetUser(Mapper.Map<ViewModelLogin, User>(login));
if (user != null && CreatePasswordHash(login.Password, user.Salt) == user.Password)
{
var ticket = new FormsAuthenticationTicket(1, login.Email, DateTime.Now, DateTime.Now.AddMinutes(20), false, "");
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
Response.Cookies.Add(cookie);
Mapper.CreateMap<User, ViewModelLogin>()
.ForMember(dest => dest.Things, opt => opt.MapFrom(src => src.Things))
.ForMember(dest => dest.Password, opt => opt.Ignore());
var returnLogin = Mapper.Map<User, ViewModelLogin>(user);
returnLogin.Success = true;
return Json(returnLogin);
}
return Json(new ViewModelLogin { Success = false, Message = "Test Message"});
}
When I do this I get an error when the serializer tries to convert the Entity to JSON. The problem is a circular dependency because the collection retains relationship information from EF (In my case a many to many relationship) apart from doing it manually has anybody else solved this issue?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该从视图模型中删除循环依赖项,或者您希望能够将其序列化为 JSON。也不要在控制器中调用
Mapper.CreateMap
。理想情况下,该方法应该仅在Application_Start
中调用一次。You should remove the circular dependency from your view model or you want be able to serialize it as JSON. Also don't call
Mapper.CreateMap<TSource, TDest>
in your controller. This method should be called only once ideally inApplication_Start
.