Controller variable is null on ajax get

Asked

Viewed 32 times

0

I have a List of objects that is instantiated when the view is created, when a value in the dropdownlist is selected, use ajax to make a request to verify which of the objects inside that List is the dropdownlist correspondent. The problem is that List is null in the ajax

public class HomeController : Controller
{
    List<Lanche> lanches = new List<Lanche>();

    ...

    public IActionResult Index()
    {    
        PopularListaLanches(lanches);
    }

    [HttpGet("GetIngredientes")]
    public JsonResult GetIngredientes(int id)
    {
        var lanche = lanches.Where(x => (int)x.NomeLanche == id).FirstOrDefault();

        return Json(lanche);
    }

there is a way to persist this data in the user session?

  • "there is a way to persist this data in the user session?" Your question already answers the question :-)

  • You can put in the session: Session["lanches"] = lanches. Another solution, if the list is the same for all users use cache or even turn the list into static: static List<Lanche> lanches = new List<Lanche>();

  • Ideally your model would search for the item when needed, using Session is an idea, but it would not be better to search for that value only when it is needed?

No answers

Browser other questions tagged

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