1

So, I have no clue how to search for an answer to this problem, mainly because there are several aspects to it which I don't know how to solve. So - here it is.

I have an object, let's call it ObjectWithItems which has a List<Item> of Item objects. Both have separate Views and Controllers (which seems logical to me, but I can't be sure).

What I'm trying to achieve is this – go to Create action of ObjectWithItemsController and fill the necessary information specific to it. Then, I click an ActionLink "Add item", which takes me to the Create action of the ItemController. The View is rendered and I enter information for that specific Item.

What I don't know how to do is the following: when I submit the current Item, it should be passed to the former Create view of the ObjectWithItems and added to the List<Item>. Furthermore, it should contain the information inputted before calling the Item Create method and the rendering of the corresponding View.

I know this is a bit blurry description, but that is because I have just started learning ASP MVC and still don't know what's what. Any tested approaches for this?

Erik Philips
  • 48,663
  • 7
  • 112
  • 142
dzenesiz
  • 935
  • 3
  • 17
  • 40
  • 1
    Do you have a database behind this ? – Antoine Pelletier Sep 12 '16 at 15:46
  • @AntoinePelletier Yes, I do. I am using EF. – dzenesiz Sep 12 '16 at 15:48
  • 1
    Well, the simple way is to add your new item right away in the db, when you get back to your ObjectWithItem, normaly the new object would appear ? – Antoine Pelletier Sep 12 '16 at 15:50
  • Yes, but this is for a university project, and one of the implementational requests is that all of the Items are saved in one transaction, so saving them one at a time and returning when a new one is added is unfortunately not an option. That is why I'm struggling here... Thank you for your help so far. – dzenesiz Sep 12 '16 at 15:57
  • 1
    Are you allowed to create temporary table ? Or use the Session["Key"] object ? Because if you don't use one of these two options well... what your asked to do is really bad... Usually you don't tranfer a whole model from a page to an other eternaly cause is a bad practice – Antoine Pelletier Sep 12 '16 at 16:00
  • I suppose. The only restrictions are that Items are saved under one transaction (all at the same time, that is) and that the Controller has no knowledge about them, meaning they are stored in a ObjectWithItems object. I know it is bad practice, but I chose MVC because I want to learn about it, so I have to bend it a little to the requirements. – dzenesiz Sep 12 '16 at 16:02
  • 1
    I would personnaly use Session["key"] object, this will save anything as long as you don't reach the timeout – Antoine Pelletier Sep 12 '16 at 16:03
  • 1
    Ok. I will read about it and use that. I suppose I am allowed to. Thank you very much. If you put that comment in an answer, I will except it. – dzenesiz Sep 12 '16 at 16:05
  • You can do all this in one view and action - refer [this answer](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) for an example. Other wise you must first save the parent and pass its ID to the method that creates the child item –  Sep 12 '16 at 22:02

2 Answers2

1

Using Session["key"] object allow you to store any kind of object, Ex :

List<Table> Rows = db.Table.Where(t => t.id < 100).ToList(); // 100 first rows

Session["TableRows"] = Rows;

And later when you want to retreive them :

List<Table> Rows = (List<Table>)Session["TableRows"] // don't forget to cast it

Your objects will exist in Session as long as you don't reach the ASP timeout

Antoine Pelletier
  • 2,764
  • 2
  • 30
  • 51
-2

Communication between View-controller is done through HTTP Post of the form.

< form class="form-horizontal" method="post" >
or @using (Html.BeginForm()) 

More info here with example

pacholik
  • 7,596
  • 8
  • 43
  • 50
win
  • 1
  • 2
  • That doesn't help at all. I know how to submit a form, everything after that is what I'm interested in. – dzenesiz Sep 12 '16 at 15:30