Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Passing value within 2 webpages using Razor syntax

0.00/5 (No votes)
14 Jun 2011 1  
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.

@{ 
    var queryValue = Request.QueryString["name"];  
 
} 
<!DOCTYPE html> 
 
<html lang="en"> 
    <head> 
        <meta charset="utf-8" /> 
        <title>Page 2</title> 
    </head> 
    <body> 
        @queryValue 
    </body> 
</html>

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

Happy coding.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here