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.










share|improve this question























  • 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















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.










share|improve this question























  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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

















  • 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
















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













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.






share|improve this answer



























    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"];





    share|improve this answer




















    • I have some reading to do on Sessions. Thank you sir.
      – Bears
      Nov 9 at 19:31










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer
























      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.






      share|improve this answer






















        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 19:17









        John Wu

        28.6k42651




        28.6k42651






















            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"];





            share|improve this answer




















            • I have some reading to do on Sessions. Thank you sir.
              – Bears
              Nov 9 at 19:31














            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"];





            share|improve this answer




















            • I have some reading to do on Sessions. Thank you sir.
              – Bears
              Nov 9 at 19:31












            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"];





            share|improve this answer












            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"];






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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
















            • 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

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20