Click here to Skip to main content
14,030,350 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

7.9K views
1 bookmarked
Posted 11 Oct 2013
Licenced CPOL

Passing value within 2 webpages using Razor syntax

, 11 Oct 2013
Rate this:
Please Sign up or sign in to vote.
Following will be the example if you would like to use Request QueryString to pass value between 2 CSHTML page.We have have 2 web pages, Page.cshtml

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Following will be the example if you would like to use Request QueryString to pass value between 2 CSHTML page.

We have have 2 web pages, Page.cshtml and Page2.cshtml. Under Page.cshtm, there will be a textbox and a button, whatever user enter in the textbox, it will be pass over to Page2.cshtml.

Here the code on Page.cshtml. We use the Request.Form to capture the input by the user from the textbox. The value then store in the variable "formValue" which will be pass over to Name under Response.Redirect.

@{ 
    var formValue = Request.Form["myTextBox"];  
    if (IsPost) 
     { 
         
        Response.Redirect("Page2.cshtml?name=" + formValue); 
         
     } 

<!DOCTYPE html> 
 
<html lang="en"> 
   
<head> 
       
<meta charset="utf-8" /> 
       
<title>Page 1</title> 
   
</head> 
   
<body> 
 
<form action="" method="post"> 
       
<input type="text" id="myTextBox" name="myTextBox"/> 
                   
<input type="submit" value="submit"/> 
</form>          
   
</body> 
</html>

Here's the code on Page2.cshtml, please take note on the previous code Response.Redirect("Page2.cshtml?name=" + formValue); we actually pass the value to the variable named "NAME". so in Page2.cshtml, we will capture the value under NAME. We used Request.QueryString to capture the value from the URL which will be http://localhost/page2.cshtml?name=WHATEVERUSERINPUT.

<span class="pln">@{ 
    var queryValue = Request.QueryString["name"];  
 
} 
</span><span class="dec"><span style="COLOR:#660066;"><!DOCTYPE html></span></span><span class="pln"> 
 
</span><span class="tag"><span style="COLOR:#000088;"><html</span></span><span class="pln"> </span><span class="atn"><span style="COLOR:#660066;">lang</span></span><span class="pun"><span style="COLOR:#666600;">=</span></span><span class="atv"><span style="COLOR:#008800;">"en"</span></span><span class="tag"><span style="COLOR:#000088;">></span></span><span class="pln"> 
    </span><span class="tag"><span style="COLOR:#000088;"><head></span></span><span class="pln"> 
        </span><span class="tag"><span style="COLOR:#000088;"><meta</span></span><span class="pln"> </span><span class="atn"><span style="COLOR:#660066;">charset</span></span><span class="pun"><span style="COLOR:#666600;">=</span></span><span class="atv"><span style="COLOR:#008800;">"utf-8"</span></span><span class="pln"> </span><span class="tag"><span style="COLOR:#000088;">/></span></span><span class="pln"> 
        </span><span class="tag"><span style="COLOR:#000088;"><title>Page 2</title></span></span><span class="pln"> 
    </span><span class="tag"><span style="COLOR:#000088;"></head></span></span><span class="pln"> 
    </span><span class="tag"><span style="COLOR:#000088;"><body></span></span><span class="pln"> 
        @queryValue 
    </span><span class="tag"><span style="COLOR:#000088;"></body></span></span><span class="pln"</span><span class="tag"><span style="COLOR:#000088;"></html></span></span>

For this example, queryValue will return the value of "WHATEVERUSERINPUT".

Happy coding.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ASP.NET Community
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
Group type: Collaborative Group

760 members


You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190419.4 | Last Updated 11 Oct 2013
Article Copyright 2013 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid