asp.net posting from view to List on server side
up vote
0
down vote
favorite
I'm a beginner. I'm still studying. I have made this code, which works as intended.
However, for each time I go back to another page, it can not, of course, save it to a list.
It disappears directly after I'm gone from this page.
The server page looks like this
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult pizaCart(string pizaName, string pizaDesc, string pizaPrice)
List<pizaModel> cartList = new List<pizaModel>();
toCart.CartList = cartList;
pizaName = Request.Form["pizaName"];
pizaDesc = Request.Form["pizaDesc"];
pizaPrice = Request.Form["pizaPrice"];
cartList.Add(new pizaModel name = pizaName, desc = pizaDesc, price = pizaPrice );
return View(toCart);
html page looks like this.
<form action="piza" method="post">
<input class="n" type="text" name="pizaName" id="pizaName" value="" /><br />
<input class="n" type="text" name="pizaDesc" id="pizaDesc" value="" /><br />
<input class="n" type="text" name="pizaPrice" id="pizaPrice" value="" /><br />
<button class="btn">add</button>
</form>
"I have tried to google it and look for it lots of places, but havent found any good enough answer"
-- hmm i probably need a loop somewhere?
As you can see, it's a very simple way to post data to list. Is it possible that I can keep adding to my list? (Maybe it has something to do with lifecycle). Thank you very much for your time.
c# asp.net asp.net-mvc
add a comment |
up vote
0
down vote
favorite
I'm a beginner. I'm still studying. I have made this code, which works as intended.
However, for each time I go back to another page, it can not, of course, save it to a list.
It disappears directly after I'm gone from this page.
The server page looks like this
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult pizaCart(string pizaName, string pizaDesc, string pizaPrice)
List<pizaModel> cartList = new List<pizaModel>();
toCart.CartList = cartList;
pizaName = Request.Form["pizaName"];
pizaDesc = Request.Form["pizaDesc"];
pizaPrice = Request.Form["pizaPrice"];
cartList.Add(new pizaModel name = pizaName, desc = pizaDesc, price = pizaPrice );
return View(toCart);
html page looks like this.
<form action="piza" method="post">
<input class="n" type="text" name="pizaName" id="pizaName" value="" /><br />
<input class="n" type="text" name="pizaDesc" id="pizaDesc" value="" /><br />
<input class="n" type="text" name="pizaPrice" id="pizaPrice" value="" /><br />
<button class="btn">add</button>
</form>
"I have tried to google it and look for it lots of places, but havent found any good enough answer"
-- hmm i probably need a loop somewhere?
As you can see, it's a very simple way to post data to list. Is it possible that I can keep adding to my list? (Maybe it has something to do with lifecycle). Thank you very much for your time.
c# asp.net asp.net-mvc
Are you persisting the data anywhere? What istoCart.CartList
?
– Boney
Nov 9 at 19:14
Hallo sir, thank you for asking. I have forgotten to say, that it is a ViewModel object defiend as static in the class where ActionResult pizaCart resides. public class ViewModels public List<pizaModel> PizasList get; set; public List<pizaModel> CartList get; set;
– Bears
Nov 9 at 19:18
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm a beginner. I'm still studying. I have made this code, which works as intended.
However, for each time I go back to another page, it can not, of course, save it to a list.
It disappears directly after I'm gone from this page.
The server page looks like this
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult pizaCart(string pizaName, string pizaDesc, string pizaPrice)
List<pizaModel> cartList = new List<pizaModel>();
toCart.CartList = cartList;
pizaName = Request.Form["pizaName"];
pizaDesc = Request.Form["pizaDesc"];
pizaPrice = Request.Form["pizaPrice"];
cartList.Add(new pizaModel name = pizaName, desc = pizaDesc, price = pizaPrice );
return View(toCart);
html page looks like this.
<form action="piza" method="post">
<input class="n" type="text" name="pizaName" id="pizaName" value="" /><br />
<input class="n" type="text" name="pizaDesc" id="pizaDesc" value="" /><br />
<input class="n" type="text" name="pizaPrice" id="pizaPrice" value="" /><br />
<button class="btn">add</button>
</form>
"I have tried to google it and look for it lots of places, but havent found any good enough answer"
-- hmm i probably need a loop somewhere?
As you can see, it's a very simple way to post data to list. Is it possible that I can keep adding to my list? (Maybe it has something to do with lifecycle). Thank you very much for your time.
c# asp.net asp.net-mvc
I'm a beginner. I'm still studying. I have made this code, which works as intended.
However, for each time I go back to another page, it can not, of course, save it to a list.
It disappears directly after I'm gone from this page.
The server page looks like this
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult pizaCart(string pizaName, string pizaDesc, string pizaPrice)
List<pizaModel> cartList = new List<pizaModel>();
toCart.CartList = cartList;
pizaName = Request.Form["pizaName"];
pizaDesc = Request.Form["pizaDesc"];
pizaPrice = Request.Form["pizaPrice"];
cartList.Add(new pizaModel name = pizaName, desc = pizaDesc, price = pizaPrice );
return View(toCart);
html page looks like this.
<form action="piza" method="post">
<input class="n" type="text" name="pizaName" id="pizaName" value="" /><br />
<input class="n" type="text" name="pizaDesc" id="pizaDesc" value="" /><br />
<input class="n" type="text" name="pizaPrice" id="pizaPrice" value="" /><br />
<button class="btn">add</button>
</form>
"I have tried to google it and look for it lots of places, but havent found any good enough answer"
-- hmm i probably need a loop somewhere?
As you can see, it's a very simple way to post data to list. Is it possible that I can keep adding to my list? (Maybe it has something to do with lifecycle). Thank you very much for your time.
c# asp.net asp.net-mvc
c# asp.net asp.net-mvc
edited Nov 9 at 19:07
Hassaan
1,75082149
1,75082149
asked Nov 9 at 19:02
Bears
112
112
Are you persisting the data anywhere? What istoCart.CartList
?
– Boney
Nov 9 at 19:14
Hallo sir, thank you for asking. I have forgotten to say, that it is a ViewModel object defiend as static in the class where ActionResult pizaCart resides. public class ViewModels public List<pizaModel> PizasList get; set; public List<pizaModel> CartList get; set;
– Bears
Nov 9 at 19:18
add a comment |
Are you persisting the data anywhere? What istoCart.CartList
?
– Boney
Nov 9 at 19:14
Hallo sir, thank you for asking. I have forgotten to say, that it is a ViewModel object defiend as static in the class where ActionResult pizaCart resides. public class ViewModels public List<pizaModel> PizasList get; set; public List<pizaModel> CartList get; set;
– Bears
Nov 9 at 19:18
Are you persisting the data anywhere? What is
toCart.CartList
?– Boney
Nov 9 at 19:14
Are you persisting the data anywhere? What is
toCart.CartList
?– Boney
Nov 9 at 19:14
Hallo sir, thank you for asking. I have forgotten to say, that it is a ViewModel object defiend as static in the class where ActionResult pizaCart resides. public class ViewModels public List<pizaModel> PizasList get; set; public List<pizaModel> CartList get; set;
– Bears
Nov 9 at 19:18
Hallo sir, thank you for asking. I have forgotten to say, that it is a ViewModel object defiend as static in the class where ActionResult pizaCart resides. public class ViewModels public List<pizaModel> PizasList get; set; public List<pizaModel> CartList get; set;
– Bears
Nov 9 at 19:18
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
When you call
new List<pizaModel>()
...you are creating a new list. A new list has zero elements.
Immediately after that you call
cartList.Add(new pizaModel name = pizaName, desc = pizaDesc, price = pizaPrice );
...which adds the current item to the list, which results in a list with one item.
There is no notion in this code of adding to an existing list, and it will never contain more than one item.
You will need to figure out some way of keeping the list from action to action. For example, you could persist the list in the browser and then post the whole list as JSON. Or you could store the list in a session variable or database table on the server side.
add a comment |
up vote
0
down vote
You may use sessions as below
Session["myPizza"] = cartList ;
Then cast it from the end of your Action result as below
var SelectedList = (List<pizaModel>)Session["myPizza"];
I have some reading to do on Sessions. Thank you sir.
– Bears
Nov 9 at 19:31
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
When you call
new List<pizaModel>()
...you are creating a new list. A new list has zero elements.
Immediately after that you call
cartList.Add(new pizaModel name = pizaName, desc = pizaDesc, price = pizaPrice );
...which adds the current item to the list, which results in a list with one item.
There is no notion in this code of adding to an existing list, and it will never contain more than one item.
You will need to figure out some way of keeping the list from action to action. For example, you could persist the list in the browser and then post the whole list as JSON. Or you could store the list in a session variable or database table on the server side.
add a comment |
up vote
3
down vote
When you call
new List<pizaModel>()
...you are creating a new list. A new list has zero elements.
Immediately after that you call
cartList.Add(new pizaModel name = pizaName, desc = pizaDesc, price = pizaPrice );
...which adds the current item to the list, which results in a list with one item.
There is no notion in this code of adding to an existing list, and it will never contain more than one item.
You will need to figure out some way of keeping the list from action to action. For example, you could persist the list in the browser and then post the whole list as JSON. Or you could store the list in a session variable or database table on the server side.
add a comment |
up vote
3
down vote
up vote
3
down vote
When you call
new List<pizaModel>()
...you are creating a new list. A new list has zero elements.
Immediately after that you call
cartList.Add(new pizaModel name = pizaName, desc = pizaDesc, price = pizaPrice );
...which adds the current item to the list, which results in a list with one item.
There is no notion in this code of adding to an existing list, and it will never contain more than one item.
You will need to figure out some way of keeping the list from action to action. For example, you could persist the list in the browser and then post the whole list as JSON. Or you could store the list in a session variable or database table on the server side.
When you call
new List<pizaModel>()
...you are creating a new list. A new list has zero elements.
Immediately after that you call
cartList.Add(new pizaModel name = pizaName, desc = pizaDesc, price = pizaPrice );
...which adds the current item to the list, which results in a list with one item.
There is no notion in this code of adding to an existing list, and it will never contain more than one item.
You will need to figure out some way of keeping the list from action to action. For example, you could persist the list in the browser and then post the whole list as JSON. Or you could store the list in a session variable or database table on the server side.
answered Nov 9 at 19:17
John Wu
28.6k42651
28.6k42651
add a comment |
add a comment |
up vote
0
down vote
You may use sessions as below
Session["myPizza"] = cartList ;
Then cast it from the end of your Action result as below
var SelectedList = (List<pizaModel>)Session["myPizza"];
I have some reading to do on Sessions. Thank you sir.
– Bears
Nov 9 at 19:31
add a comment |
up vote
0
down vote
You may use sessions as below
Session["myPizza"] = cartList ;
Then cast it from the end of your Action result as below
var SelectedList = (List<pizaModel>)Session["myPizza"];
I have some reading to do on Sessions. Thank you sir.
– Bears
Nov 9 at 19:31
add a comment |
up vote
0
down vote
up vote
0
down vote
You may use sessions as below
Session["myPizza"] = cartList ;
Then cast it from the end of your Action result as below
var SelectedList = (List<pizaModel>)Session["myPizza"];
You may use sessions as below
Session["myPizza"] = cartList ;
Then cast it from the end of your Action result as below
var SelectedList = (List<pizaModel>)Session["myPizza"];
answered Nov 9 at 19:25
PrathapG
467316
467316
I have some reading to do on Sessions. Thank you sir.
– Bears
Nov 9 at 19:31
add a comment |
I have some reading to do on Sessions. Thank you sir.
– Bears
Nov 9 at 19:31
I have some reading to do on Sessions. Thank you sir.
– Bears
Nov 9 at 19:31
I have some reading to do on Sessions. Thank you sir.
– Bears
Nov 9 at 19:31
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231837%2fasp-net-posting-from-view-to-list-on-server-side%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you persisting the data anywhere? What is
toCart.CartList
?– Boney
Nov 9 at 19:14
Hallo sir, thank you for asking. I have forgotten to say, that it is a ViewModel object defiend as static in the class where ActionResult pizaCart resides. public class ViewModels public List<pizaModel> PizasList get; set; public List<pizaModel> CartList get; set;
– Bears
Nov 9 at 19:18