Simple Razor Example

Step 1: First we create a webpage named page1.cshtml

Step 2: Then we create Two TextBox (Id,Name) and Two Labels and a Submit Button.

<div>
<label for="Id">Id:</label>
<input type="text" name="Id" value="" />
</div>
<div>
<label for="Name">Name:</label>
<input type="text" name="Name" value="" />
</div>
<div>
<input type="submit" value="Submit" class="submit" />
</div>


Step 3: Then we write the following code:

@{
if (IsPost) {
string Id = Request["Id"];
string Name = Request["Name"];


<text>


Your values are:
Id: @Id <br />
Name: @Name

</text>
}
}

Note:

Here IsPost method is used to check that the page is posted or not. When we click on the submit button it checks if the page is being posted the
code will be execute.

The Request Method is to get the values which we filled in the form (Id,Name). With the help of name (attribute) it gets the value of the Control.
In this Case (Id,Name)

If there are any integer field in it like marks we can write Request method like this:

int marks1 = Request["marks1"].AsInt();


AsInt method converts the value to the Integer


@{
if (IsPost) {
string Id = Request["Id"];
string Name = Request["Name"];


<text>


Your values are:
Id: @Id <br />
Name: @Name

</text>
}
}

<html>
<head>
<title>My Form</title>
</head>
<body>
<form method="post" action="">

<div>
<label for="Id">Id:</label>
<input type="text" name="Id" value="" />
</div>
<div>
<label for="Name">Name:</label>
<input type="text" name="Name" value="" />
</div>
<div>
<input type="submit" value="Submit" class="submit" />
</div>

</form>
</body>
</html>
Ebook Download
View all
Learn
View all