locked
How to pass a variable from one page.cshtml to another RRS feed

  • Question

  • User-209869480 posted

    How to pass a variable from one page.cshtml to another?

    Thanks in advance,

    Dimitris

    Wednesday, September 11, 2013 7:37 AM

Answers

  • User281315223 posted

    There are a few different methods that you can use depending if you want to pass the values through a <form> submission or through the QueryString.

    The simplest method would be to simply create a <form> element that targets another Page of yours using the "action" attribute : 

    <!-- Example Form -->
    <form action='YourTargetPage.cshtml' method='post'>
        <!-- Example element to pass across -->
        <input name='YourValue' value='Test' />
    
        <!-- Button to submit your form -->
        <input type='submit' value='Submit' />
    </form>

    This will actually POST your values to YourTargetPage and within the Request, you'll be able to access the value of any of your elements that contained a name attribute by that name property as such. You would have something like this within your YourTargetPage.cshml file : 

    //Grab your value from the Request Collection (if it exists)
    if(Request.Form["YourValue"] != null)
    {
          //Grab your value (since it exists)
          var yourValue = Request.Form["YourValue"];
    }

    Alternatively, you could perform your navigation through your code-behind (after your form is initially submitted) by using the Session or the QueryString to store your values as such : 

    //Pass your value within the QueryString (using the YourValue key)
    Response.Redirect(String.Format("YourTargetPage.cshtml?YourValue={0}",yourValueToPass));
    
    //This would be retrieved in your YourTargetPage.cshtml file using the following code
    if(Request.QueryString["YourValue"] != null)
    {
          //Grab your value if it exists
          var yourValue = Request.QueryString["YourValue"];
    }

    and finally you could store your value within the Session prior to manually redirecting your page : 

    //Store your value within the Session
    Session["YourValue"] = yourValueToPass;
    
    //Redirect to your other page after storing your value
    Response.Redirect("YourTargetPage.cshtml");
    
    //This would be retrieved in your YourTargetPage.cshtml file using the following code
    if(Session["YourValue"] != null)
    {
          //Grab your value if it exists
          var yourValue = Session["YourValue"];
    }






    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 11, 2013 8:29 AM

All replies

  • User2019981500 posted

    hi

    What are you having difficulties with? On one page you create a 

    <form>

    :<form action="/foo.cshtml" method="post"> <input type="text" name="bar" value="" /> <button type="submit">OK</button> </form>

    and on the other page you read values from the request: 

    Request["bar"]

    :@{ var bar = Request["bar"]; }

    stackoverflow
    Wednesday, September 11, 2013 7:47 AM
  • User281315223 posted

    There are a few different methods that you can use depending if you want to pass the values through a <form> submission or through the QueryString.

    The simplest method would be to simply create a <form> element that targets another Page of yours using the "action" attribute : 

    <!-- Example Form -->
    <form action='YourTargetPage.cshtml' method='post'>
        <!-- Example element to pass across -->
        <input name='YourValue' value='Test' />
    
        <!-- Button to submit your form -->
        <input type='submit' value='Submit' />
    </form>

    This will actually POST your values to YourTargetPage and within the Request, you'll be able to access the value of any of your elements that contained a name attribute by that name property as such. You would have something like this within your YourTargetPage.cshml file : 

    //Grab your value from the Request Collection (if it exists)
    if(Request.Form["YourValue"] != null)
    {
          //Grab your value (since it exists)
          var yourValue = Request.Form["YourValue"];
    }

    Alternatively, you could perform your navigation through your code-behind (after your form is initially submitted) by using the Session or the QueryString to store your values as such : 

    //Pass your value within the QueryString (using the YourValue key)
    Response.Redirect(String.Format("YourTargetPage.cshtml?YourValue={0}",yourValueToPass));
    
    //This would be retrieved in your YourTargetPage.cshtml file using the following code
    if(Request.QueryString["YourValue"] != null)
    {
          //Grab your value if it exists
          var yourValue = Request.QueryString["YourValue"];
    }

    and finally you could store your value within the Session prior to manually redirecting your page : 

    //Store your value within the Session
    Session["YourValue"] = yourValueToPass;
    
    //Redirect to your other page after storing your value
    Response.Redirect("YourTargetPage.cshtml");
    
    //This would be retrieved in your YourTargetPage.cshtml file using the following code
    if(Session["YourValue"] != null)
    {
          //Grab your value if it exists
          var yourValue = Session["YourValue"];
    }






    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 11, 2013 8:29 AM
  • User-209869480 posted

    Thanks to all !!!

    Wednesday, September 11, 2013 9:24 AM