0

I'm new to C#, but when I'm submitting a form, I catch the form values and create a new item of it and put it in a list according to the code below:

    public ActionResult Movie()
    {

        int id = Convert.ToInt16(Request.Form["inputmovieid"]);
        string name = Request.Form["inputmovietitle"];
        int year = Convert.ToInt16(Request.Form["inputproductionyear"]);

        List<Movie> movies = new List<Movie>();            

        Movie movieitem = new Movie(id, name, year);

        movies.Add(movieitem);

        return View(movies);
   }

In a .cshtml-file I then print the created item with a foreach loop, but the problem is that if I submit this form again with new values for a new item, it overwrites the old one in the list.

DESIRED RESULT: When submitting the form again I want a new item to be added to the list without overwriting the old one. Does anyone know how the code above can be modified to solve this?

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
eqinna
  • 321
  • 3
  • 5
  • 14
  • 4
    Right now, you get a new list on every request. You're going to need to involve some storage somewhere to keep page state. The list of movies then needs to be put in that state. It could be a simple session in memory, or a database, or client side javascript-based storage etc. – James Thorpe Sep 19 '16 at 12:10
  • 2
    The problem is that you never store the created / added item. What you're doing here is creating a new list every time. And after that, you add one item in a newly created list and "display" it. – Razvan Dumitru Sep 19 '16 at 12:10
  • I'm new to C#, so could you please give a code sample? – eqinna Sep 19 '16 at 12:28

1 Answers1

1

You need to store the data into a database of keep track or a list inside a ViewState.

Something like:

public ActionResult Movie()
{
    var list = ViewState["MyList"] as List<Movie>;
    if(list == null)
    {
       list = new List<Movie>();
       ViewState["MyList"] = list;
    }
    int id = Convert.ToInt16(Request.Form["inputmovieid"]);
    string name = Request.Form["inputmovietitle"];
    int year = Convert.ToInt16(Request.Form["inputproductionyear"]);



    Movie movieitem = new Movie(id, name, year);

    list.Add(movieitem);

    return View(list);
}

But my knowledge of asp.net has faded a little, you might need to mark the Movie class as serializable,

[Serializable]
public class Movie
{
    ...
}

Every time you get a postback/reload, the whole class is reconstructed, so a storing a list into a field won't help you. That's why they invented the viewstate. (hidden formfield sent with the html)

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
  • In this case I want to solve it without using a database, I simply want to be able to add several items to a list on submit without overwriting an old one, but I cannot make it work even if I put the list outside the method. :-( – eqinna Sep 19 '16 at 12:39
  • You might reread my answer, it is not storing it in a field, it stores it into the viewstate. (no database is involved) – Jeroen van Langen Sep 19 '16 at 12:40
  • When I copied-pasted your code It complains about "ViewState does not exist in the current context". I'm really new to C# so I have no clue what that means? – eqinna Sep 19 '16 at 12:45
  • I might assume something, are you doing asp.net? (I added the tag) The surrounding class, can you show the definition? – Jeroen van Langen Sep 19 '16 at 12:47
  • Yes, it's C# with asp.net. – eqinna Sep 19 '16 at 12:49
  • You need to add more info about where the `public ActionResult Movie()` is located – Jeroen van Langen Sep 19 '16 at 12:53
  • It's located in a contoller class called "DefaultController" in the controller layer. I build the script according to MVC. – eqinna Sep 19 '16 at 12:55
  • You might take a look here: http://stackoverflow.com/questions/23623229/what-is-the-equivalent-of-viewstate-in-asp-net-mvc **for the next time, add tags to your question, against bad assumptions** – Jeroen van Langen Sep 19 '16 at 12:58
  • Great help this – Kinyanjui Kamau Nov 14 '19 at 14:02