为什么使用 List<> 时相关表值在 HttpContext.Session.Get

我正在开发一个购物车(使用 MVC Asp.Net Core 2.2 和 EF)。

有3个表(模型类):

  • 产品;

  • 尺寸(与产品表相关);

  • TipoMassa(与 Produto 表相关)。

在 ProductController 类中,我创建了一个存储在 Session 中的列表,以便在 CartController 类中获取此列表。

但是,当我尝试在 CartController 类中获取该列表时,相关表(Tamanho 和 TipoMassa)中的值为空。该列表仅包含 Produto 表值。

我该如何修复它?

控制器类产品

public ActionResult AddToCart(IFormCollection collection)

{

    string Nome = collection["Nome"];

    byte IdTamanho = Convert.ToByte(collection["IdTamanho"]);

    byte IdTipoMassa = Convert.ToByte(collection["IdTipoMassa"]);



    var produtoContext = _context.Produto

            .Include(c => c.IdCategoriaNavigation)

            .Include(c => c.IdTamanhoNavigation)

            .Include(c => c.IdTipoMassaNavigation)

            .FirstOrDefault(p => p.Nome == Nome && p.IdTamanho == IdTamanho

        && p.IdTipoMassa == IdTipoMassa);


    if (produtoContext == null)

    {

        return NotFound();

    }


    // Add itens na Sesssion

    List<Produto> itens = new List<Produto>();

    itens = HttpContext.Session.Get<List<Produto>>("itens");

    if (itens == null)

    {

        itens = new List<Produto>();

    }


    itens.Add(produtoContext);

    HttpContext.Session.Set("itens", itens);

    TempData["save"] = "Adicionado com sucesso";


    return RedirectToAction(nameof(Index), "Produto");

}

工作正常!但在 [CartController] IdCategoriaNavigation、IdTamanhoNavigation 和 IdTipoMassaNavigation 为空。代码如下。

购物车控制器类

public IActionResult Index()

{

    List<Produto> itens = HttpContext.Session.Get<List<Produto>>

            ("itens");

    if (itens == null)

    {

        itens  = new List<Produto>();

    }

    return View(itens.ToList());

}

当我尝试在视图中显示值时,相关表值(IdCategoriaNavigation、IdTamanhoNavigation 和 IdTipoMassaNavigation)为空。代码如下。

产品型号类别

public partial class Produto

{

    public Produto()

    {

        Cardapio = new HashSet<Cardapio>();

        ItensPedido = new HashSet<ItensPedido>();

        Promocao = new HashSet<Promocao>();

    }

汪汪一只猫
浏览 110回答 2
2回答

繁花如伊

除非我弄错了,否则我相信问题就出在 lambda 函数上:代替@Html.DisplayFor(modelItem&nbsp;=>&nbsp;item.IdTamanhoNavigation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^您需要引用正确的输入参数(modelItem):@Html.DisplayFor(modelItem&nbsp;=>&nbsp;modelItem.IdTamanhoNavigation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^

慕标琳琳

我忘了说我一直在使用 JSon 将该列表序列化为会话。但我必须在 Produto Model 类的那些相关方法上使用 [JsonIgnore],如果没有,当包含这些相关表值时,我会从 Newtonsoft.Jason 收到错误。正如你在下面看到的在 ShoppingCart 控制器类上public ActionResult AddToCart(IFormCollection collection){&nbsp; &nbsp; ...&nbsp; &nbsp; var produtoContext = _context.Produto&nbsp; &nbsp; &nbsp; &nbsp; .Include(c => c.IdCategoriaNavigation)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^&nbsp; &nbsp; &nbsp; &nbsp; .Include(c => c.IdTamanhoNavigation)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^&nbsp; &nbsp; &nbsp; &nbsp; .Include(c => c.IdTipoMassaNavigation)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^&nbsp; &nbsp; &nbsp; &nbsp;.FirstOrDefault(p => p.Nome == Nome && p.IdTamanho == IdTamanho&nbsp;&nbsp; &nbsp; && p.IdTipoMassa == IdTipoMassa);&nbsp; &nbsp; ...&nbsp;&nbsp; &nbsp; The produtoContex here, normally has all the values from the related tables.&nbsp; &nbsp; &nbsp; &nbsp; {PianoPizza.Models.Categoria}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {PianoPizza.Models.Tamanho}&nbsp; &nbsp; &nbsp; &nbsp; {PianoPizza.Models.TipoMassa}&nbsp; &nbsp; &nbsp; &nbsp; But when I set the List on Session I got the Json error.&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; HttpContext.Session.Set("itens", itens);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^&nbsp; &nbsp; ...&nbsp;}JsonSerializationException:检测到类型为“Pianino.Models.Produto”的自引用循环。路径“[0].IdCategoriaNavigation.Produto”。Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer、对象值、JsonProperty 属性、JsonContract 合约、JsonContainerContract 容器合约、JsonProperty 容器属性)即使评论第一个包括,我对 IdTamanhoNavigation 和 IdTipoMassaNavigation 也有相同的看法。因此,使用如下所示的 [JsonIgnore],我修复了该错误。另一方面,当我从 ShoppingCart Controller 类上的会话获取列表时,这些值变为 null。关于产品型号类&nbsp;[JsonIgnore]&nbsp;public virtual Categoria IdCategoriaNavigation { get; set; }&nbsp;[JsonIgnore]&nbsp;public virtual Tamanho IdTamanhoNavigation { get; set; }&nbsp;[JsonIgnore]&nbsp;public virtual TipoMassa IdTipoMassaNavigation { get; set; }在实用程序类上public static class SessionExtensions{&nbsp; &nbsp; public static void Set<T>(this ISession session, string key, T value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; session.SetString(key, JsonConvert.SerializeObject(value));&nbsp; &nbsp; }&nbsp; &nbsp; public static T Get<T>(this ISession session, string key)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var value = session.GetString(key);&nbsp; &nbsp; &nbsp; &nbsp; return value == null ? default(T) :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonConvert.DeserializeObject<T>(value);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP