A form is the part of an HTML document where input controls (text boxes, check boxes, radio buttons, drop-down lists) are placed.
Create an HTML input page
Razor Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <html>
<body>
@{
if (IsPost) {
string companyname = Request[ "companyname" ];
string contactname = Request[ "contactname" ];
<p>You entered: <br />
Company Name: @companyname <br />
Contact Name: @contactname </p>
}
else
{
<form method= "post" action= "" >
Company Name:<br />
<input type= "text" name= "CompanyName" value= "" /><br />
Contact Name:<br />
<input type= "text" name= "ContactName" value= "" /><br /><br />
<input type= "submit" value= "Submit" class = "submit" />
</form>
}
}
</body>
</html>
|
Copy after login
Razor Example - Display Image
Assume that in your There are 3 images in the images folder and you want to display the images dynamically based on the user's selection.
This can be achieved with a simple piece of Razor code.
If you have an image named "Photo1.jpg" in your website's images folder, you can use the HTML
element to display the image, like this:
1 | <img src= "images/Photo1.jpg" alt= "Sample" />
|
Copy after login
The following example demonstrates how to display an image selected by the user from the following list:
Razor Example
1 | @{ var imagePath= "" ; if (Request[ "Choice" ] != null){imagePath= "images/" + Request[ "Choice" ];} } <!DOCTYPE html> <html> <body> <h1>Display Images</h1> <form method= "post" action= "" > I want to see: <select name= "Choice" > <option value= "Photo1.jpg" >Photo 1</option> <option value= "Photo2.jpg" >Photo 2</option> <option value= "Photo3.jpg" >Photo 3</option> </select> <input type= "submit" value= "Submit" /> @ if (imagePath != "" ){<p><img src= "@imagePath" alt= "Sample" /></p>} </form> </body> </html>
|
Copy after login
Example Explanation
The server creates a file called Variable of imagePath.
The HTML page has a drop-down list (