从 MVC 操作以 JSON 形式返回带有 Children 的 EF 实体

发布于 2024-10-26 23:09:20 字数 1605 浏览 1 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗷兮 2024-11-02 23:09:20

您应该从视图模型中删除循环依赖项,或者您希望能够将其序列化为 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 in Application_Start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文