0

Hello for some reason I'm able to add items to a list in one Action method but whenever I pass the list to a view in another method the list appears to be EMPTY. Why is this and could someone explain this? Why is the list empty when it has been added something in another method before?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Hotel.Models;
using System.Diagnostics;

namespace HotelTwee.Controllers
{
    public class HomeController : Controller
    {

        public List<Hotel> preOrders = new List<Hotel>();


        public IActionResult Index()
        {    
            // List Count is ZERO here
            return View(preOrders);
        }


        [HttpGet]
        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Create(Hotel order)
        {
            preOrders.Add(order);
            // List COUNT is ONE here
            return RedirectToAction("Index", preOrders);               
        }
    }
}

EDIT:

Removing RedirectToAction and using return View("Index", preOrders) has fixed the problem

  • 1
    Your controller is instantiated for every request, it doesn't keep any state. – Simon Karlsson Nov 23 '17 at 14:30
  • But I have to use a list for my excercise. How can I use List instead of a Database, in this case, without having it within the controller? – John Reagan Nov 23 '17 at 14:32
  • Save the data to a persistent medium (ex : database) and read it again in the Index action. – Shyju Nov 23 '17 at 14:32
  • making the list `static` would solve your problem, however that is probably not a good idea for concurrency reasons – Simon Karlsson Nov 23 '17 at 14:33
  • the point of an exercise is to learn; look at the docs and the infinite number of tutorials available and see how people do this sort of thing instead of coming here for a solution – The Bearded Llama Nov 23 '17 at 14:33
  • You can use TempData to transfer data. Take a look [How do I include a model with a RedirectToAction?](https://stackoverflow.com/questions/11209191/how-do-i-include-a-model-with-a-redirecttoaction/11209320#11209320) – Shyju Nov 23 '17 at 14:33
  • 3
    Possible duplicate of [How can I maintain ModelState with RedirectToAction?](https://stackoverflow.com/questions/279665/how-can-i-maintain-modelstate-with-redirecttoaction) – Bernard Vander Beken Nov 23 '17 at 14:33
  • Not intentionally Bernard. – John Reagan Nov 23 '17 at 14:35
  • Keep in mind that, [you cannot pass complex objects in TempData in Asp.Net core](https://stackoverflow.com/a/47169878/40521) – Shyju Nov 23 '17 at 14:35
  • @Shyju Thank you for your help. – John Reagan Nov 23 '17 at 14:36

1 Answers1

0

RedirectToAction returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action.

So the state of your controller/view is gone.

Also, the second parameter of RedirectToAction is intended for the routeValues.

https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoaction(v=vs.118).aspx

Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76