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"];
}
<span style="COLOR:#660066;"><!DOCTYPE html></span>
<span style="COLOR:#000088;"><html</span> <span style="COLOR:#660066;">lang</span><span style="COLOR:#666600;">=</span><span style="COLOR:#008800;">"en"</span><span style="COLOR:#000088;">></span>
<span style="COLOR:#000088;"><head></span>
<span style="COLOR:#000088;"><meta</span> <span style="COLOR:#660066;">charset</span><span style="COLOR:#666600;">=</span><span style="COLOR:#008800;">"utf-8"</span> <span style="COLOR:#000088;">/></span>
<span style="COLOR:#000088;"><title>Page 2</title></span>
<span style="COLOR:#000088;"></head></span>
<span style="COLOR:#000088;"><body></span>
@queryValue
<span style="COLOR:#000088;"></body></span>
<span style="COLOR:#000088;"></html></span>
For this example, queryValue will return the value of "WHATEVERUSERINPUT".
Happy coding.
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.