How to pass a value between controller actions












0















I have a page with several partial views. Each one contains a Ajax.BeginForm and all post to the same controller to perform various lookups. The ActionResult returns a value that I need to store for use in another ActionResult on the same page, same controller.



I've tried TempData, ViewBag, ViewData but the value is not being passed.



I have a typical controller action like so:



    public ActionResult ExistingLogin(AMSIdentifyInputModel_ExistingLogin model)
{
if (ModelState.IsValid)
{
int? entityNumber = (from s in statebarDB.IMail
where s.USERID == model.oldUsername
&& s.PASSWORD == model.oldPassword
select s.ENTITY_NUMBER).FirstOrDefault();

if (entityNumber != null && entityNumber != 0)
{
ViewData["entityNumber"] = entityNumber;
return Json(new { result = 1, message = "User found." });
}
}
}


and another partial view that submits to a different action and tries to read that entityNumber with no success:



    [ValidateAntiForgeryToken]
[HttpPost]
public ActionResult CreateAccount(AMSIdentify_RegisterInputModel model)
{
if (ModelState.IsValid)
{
this.userAccountService.UpdateEntityID(account, (int)ViewData["entityNumber"]);
. . .


Is there another way to store a value with the view temporarily?










share|improve this question

























  • Tempdata is the only one option that can work for you as in your question you are using Viewdata which lost its value on redirect...but tempdata can retain its value for one redirect...

    – Kartikeya Khosla
    Aug 6 '14 at 4:49


















0















I have a page with several partial views. Each one contains a Ajax.BeginForm and all post to the same controller to perform various lookups. The ActionResult returns a value that I need to store for use in another ActionResult on the same page, same controller.



I've tried TempData, ViewBag, ViewData but the value is not being passed.



I have a typical controller action like so:



    public ActionResult ExistingLogin(AMSIdentifyInputModel_ExistingLogin model)
{
if (ModelState.IsValid)
{
int? entityNumber = (from s in statebarDB.IMail
where s.USERID == model.oldUsername
&& s.PASSWORD == model.oldPassword
select s.ENTITY_NUMBER).FirstOrDefault();

if (entityNumber != null && entityNumber != 0)
{
ViewData["entityNumber"] = entityNumber;
return Json(new { result = 1, message = "User found." });
}
}
}


and another partial view that submits to a different action and tries to read that entityNumber with no success:



    [ValidateAntiForgeryToken]
[HttpPost]
public ActionResult CreateAccount(AMSIdentify_RegisterInputModel model)
{
if (ModelState.IsValid)
{
this.userAccountService.UpdateEntityID(account, (int)ViewData["entityNumber"]);
. . .


Is there another way to store a value with the view temporarily?










share|improve this question

























  • Tempdata is the only one option that can work for you as in your question you are using Viewdata which lost its value on redirect...but tempdata can retain its value for one redirect...

    – Kartikeya Khosla
    Aug 6 '14 at 4:49
















0












0








0








I have a page with several partial views. Each one contains a Ajax.BeginForm and all post to the same controller to perform various lookups. The ActionResult returns a value that I need to store for use in another ActionResult on the same page, same controller.



I've tried TempData, ViewBag, ViewData but the value is not being passed.



I have a typical controller action like so:



    public ActionResult ExistingLogin(AMSIdentifyInputModel_ExistingLogin model)
{
if (ModelState.IsValid)
{
int? entityNumber = (from s in statebarDB.IMail
where s.USERID == model.oldUsername
&& s.PASSWORD == model.oldPassword
select s.ENTITY_NUMBER).FirstOrDefault();

if (entityNumber != null && entityNumber != 0)
{
ViewData["entityNumber"] = entityNumber;
return Json(new { result = 1, message = "User found." });
}
}
}


and another partial view that submits to a different action and tries to read that entityNumber with no success:



    [ValidateAntiForgeryToken]
[HttpPost]
public ActionResult CreateAccount(AMSIdentify_RegisterInputModel model)
{
if (ModelState.IsValid)
{
this.userAccountService.UpdateEntityID(account, (int)ViewData["entityNumber"]);
. . .


Is there another way to store a value with the view temporarily?










share|improve this question
















I have a page with several partial views. Each one contains a Ajax.BeginForm and all post to the same controller to perform various lookups. The ActionResult returns a value that I need to store for use in another ActionResult on the same page, same controller.



I've tried TempData, ViewBag, ViewData but the value is not being passed.



I have a typical controller action like so:



    public ActionResult ExistingLogin(AMSIdentifyInputModel_ExistingLogin model)
{
if (ModelState.IsValid)
{
int? entityNumber = (from s in statebarDB.IMail
where s.USERID == model.oldUsername
&& s.PASSWORD == model.oldPassword
select s.ENTITY_NUMBER).FirstOrDefault();

if (entityNumber != null && entityNumber != 0)
{
ViewData["entityNumber"] = entityNumber;
return Json(new { result = 1, message = "User found." });
}
}
}


and another partial view that submits to a different action and tries to read that entityNumber with no success:



    [ValidateAntiForgeryToken]
[HttpPost]
public ActionResult CreateAccount(AMSIdentify_RegisterInputModel model)
{
if (ModelState.IsValid)
{
this.userAccountService.UpdateEntityID(account, (int)ViewData["entityNumber"]);
. . .


Is there another way to store a value with the view temporarily?







ajax asp.net-mvc asp.net-mvc-partialview tempdata






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 6 '14 at 2:51









tereško

52.3k2077135




52.3k2077135










asked Aug 5 '14 at 22:28









Connie DeCinkoConnie DeCinko

4501926




4501926













  • Tempdata is the only one option that can work for you as in your question you are using Viewdata which lost its value on redirect...but tempdata can retain its value for one redirect...

    – Kartikeya Khosla
    Aug 6 '14 at 4:49





















  • Tempdata is the only one option that can work for you as in your question you are using Viewdata which lost its value on redirect...but tempdata can retain its value for one redirect...

    – Kartikeya Khosla
    Aug 6 '14 at 4:49



















Tempdata is the only one option that can work for you as in your question you are using Viewdata which lost its value on redirect...but tempdata can retain its value for one redirect...

– Kartikeya Khosla
Aug 6 '14 at 4:49







Tempdata is the only one option that can work for you as in your question you are using Viewdata which lost its value on redirect...but tempdata can retain its value for one redirect...

– Kartikeya Khosla
Aug 6 '14 at 4:49














2 Answers
2






active

oldest

votes


















0














Assuming entityNumber is just an ID, and not a massively important security hole or enormous gigabyte-long number:



Assignment:



Session["entityNumber"] = entityNumber;


Reference:



(int)Session["entityNumber"];





share|improve this answer































    0














    There are 2 other ways to store data in a temporary manner that is on the frontend side.
    1. You can use LocalStorage functionality given by HTML5 using javascript or JQuery.
    2. You can store the value in a model value and at the time of execution, you can pass the value with the variable, in javascript side.



    On the backend side, TempData, ViewBag, ViewData are loaded at the time of page load and hence they are not able to change as per our need.



    Thus we can use Session if you need to use it in backend side.
    Also you can use the Cookies too.






    share|improve this answer























      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',
      autoActivateHeartbeat: false,
      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%2f25149366%2fhow-to-pass-a-value-between-controller-actions%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









      0














      Assuming entityNumber is just an ID, and not a massively important security hole or enormous gigabyte-long number:



      Assignment:



      Session["entityNumber"] = entityNumber;


      Reference:



      (int)Session["entityNumber"];





      share|improve this answer




























        0














        Assuming entityNumber is just an ID, and not a massively important security hole or enormous gigabyte-long number:



        Assignment:



        Session["entityNumber"] = entityNumber;


        Reference:



        (int)Session["entityNumber"];





        share|improve this answer


























          0












          0








          0







          Assuming entityNumber is just an ID, and not a massively important security hole or enormous gigabyte-long number:



          Assignment:



          Session["entityNumber"] = entityNumber;


          Reference:



          (int)Session["entityNumber"];





          share|improve this answer













          Assuming entityNumber is just an ID, and not a massively important security hole or enormous gigabyte-long number:



          Assignment:



          Session["entityNumber"] = entityNumber;


          Reference:



          (int)Session["entityNumber"];






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 6 '14 at 8:54









          Inspector SquirrelInspector Squirrel

          2,45522037




          2,45522037

























              0














              There are 2 other ways to store data in a temporary manner that is on the frontend side.
              1. You can use LocalStorage functionality given by HTML5 using javascript or JQuery.
              2. You can store the value in a model value and at the time of execution, you can pass the value with the variable, in javascript side.



              On the backend side, TempData, ViewBag, ViewData are loaded at the time of page load and hence they are not able to change as per our need.



              Thus we can use Session if you need to use it in backend side.
              Also you can use the Cookies too.






              share|improve this answer




























                0














                There are 2 other ways to store data in a temporary manner that is on the frontend side.
                1. You can use LocalStorage functionality given by HTML5 using javascript or JQuery.
                2. You can store the value in a model value and at the time of execution, you can pass the value with the variable, in javascript side.



                On the backend side, TempData, ViewBag, ViewData are loaded at the time of page load and hence they are not able to change as per our need.



                Thus we can use Session if you need to use it in backend side.
                Also you can use the Cookies too.






                share|improve this answer


























                  0












                  0








                  0







                  There are 2 other ways to store data in a temporary manner that is on the frontend side.
                  1. You can use LocalStorage functionality given by HTML5 using javascript or JQuery.
                  2. You can store the value in a model value and at the time of execution, you can pass the value with the variable, in javascript side.



                  On the backend side, TempData, ViewBag, ViewData are loaded at the time of page load and hence they are not able to change as per our need.



                  Thus we can use Session if you need to use it in backend side.
                  Also you can use the Cookies too.






                  share|improve this answer













                  There are 2 other ways to store data in a temporary manner that is on the frontend side.
                  1. You can use LocalStorage functionality given by HTML5 using javascript or JQuery.
                  2. You can store the value in a model value and at the time of execution, you can pass the value with the variable, in javascript side.



                  On the backend side, TempData, ViewBag, ViewData are loaded at the time of page load and hence they are not able to change as per our need.



                  Thus we can use Session if you need to use it in backend side.
                  Also you can use the Cookies too.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 9:33









                  Yagnesh KhamarYagnesh Khamar

                  215




                  215






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25149366%2fhow-to-pass-a-value-between-controller-actions%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

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      Npm cannot find a required file even through it is in the searched directory