Add multiple items in a single Session

Asked

Viewed 119 times

0

That way I add an item to session

 var item = new FornecedorTipoFrete()
            {

                TipoFreteId = id,
                TipoFrete = new TipoFrete() { Descricao = tipofrete },
                FornecedorId = (fornecedorID)
            };

            HttpContext.Session.SetString("TipoFreteId", id.ToString());

However, this process can occur several times. The user can add multiple Types, so it could be one or several, as I can add all and then read this session with multiple items.

EDIT

I tried to do it this way: I added this line to controller

IList<string> listaFrete = new List<string>();

and then to save in Session did it this way:

listaFrete.Add(id.ToString());
            HttpContext.Session.SetObject("Lista", listaFrete);

But he always picks up the last one, doesn’t save it in the list. There’s always only one item, always the last one.

  • cannot add an array or list of "Tipofreteid"?

  • The form today accepts only one Typofreteid, the user will be able to include several, and will only save when Submit is performed, if no problems occur, can.

  • so I think you can fix it.. It will only take one more job to treat the list, and maybe remove/add items depending on if you have changes

  • I need to add, add via Json, and to delete also delete via Json.

  • if you have a lot of maintenance maybe Session is not the best place, maybe a bank is better

  • In this case, it is pq the user will include to save, he can include and decide not to terminate the registration. Would have some better way to do?

  • Session may lose if it closes the browser or it may expire.. a possibility would be to save to a "temporary" table or if you have a database in your environment for example. are options

  • In case I think the session would be better.

  • Use cookies if this is the case. But still if you prefer to use Session I suggest saving an item list or array to Session

  • @Ricardopunctual I edited the question.

Show 5 more comments

1 answer

1


First is your method that you practically already use to add the Session, and then the method you will use to search for an item in the list stored in the Session.

void AddItemSession()
{
  var item = new FornecedorTipoFrete()
  {

    TipoFreteId = id,
    TipoFrete = new TipoFrete() { Descricao = tipofrete },
    FornecedorId = (fornecedorID)
  };

  List<FornecedorTipoFrete> lstFornecedorTipoFrete = new List<FornecedorTipoFrete>();

  if (Session["ListaTipoFrete"] != null)
        lstFornecedorTipoFrete = (List<FornecedorTipoFrete>)Session["ListaTipoFrete"];
        
  lstFornecedorTipoFrete.Add(item);
  Session["ListaTipoFrete"] = lstFornecedorTipoFrete;
}

FornecedorTipoFrente ReturnFornecedorTipoFrete(string tipoFreteId)
{
  if (Session["ListaTipoFrete"] == null)
    return null;

  return (Session["ListaTipoFrete"] as List<FornecedorTipoFrete>).FirstOrDefault(c=> c.TipoFreteId == tipoFreteId);
}

  • I don’t have the option of Session, using Asp.net core, he informs that there is no Session in the current context

  • Use Httpcontext.session.Setstring() then arrow a json using Jsonconvert.Serializeobject(value). The logic is the same.

  • I tried adding this line in the controller IList<string> listaFrete = new List<string>(); and then doing so to get the value. listaFrete.Add(id.ToString());&#xA; HttpContext.Session.SetObject("Lista", listaFrete); but it always replaced, and does not include in the list.

  • So always arrow in a string only separating the ids by "," (Comma). Ex: "1,17,9". always seeking the information and giving . Split(',') retrieving an array of ids.

  • I edited the question, how I’m trying to do it.

  • It’s always getting the last one isn’t it because it always creates a new list? Ilist<string> listFrete = new List<string>();

  • I didn’t create inside the function. Is it still running?

  • I just declared so IList<int> listaFrete; and within the function I do so: if (listaFrete == null)&#xA; {&#xA; listaFrete = new List<int>();&#xA; } but he’s always returning null

  • 1

    put with static and it worked, thank you.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.