@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<h1>Welcome to Razor Pages</h1>
<p>This is a simple example of a Razor Page in C#.</p>
<!-- This is a form that posts data to the server -->
<form method="post">
<label for="name">Enter your name:</label>
<input type="text" id="name" name="name" />
<button type="submit">Submit</button>
</form>
@if (!string.IsNullOrEmpty(Model.Name))
{
<p>Hello, @Model.Name!</p>
}
@code {
// This is the code-behind section where you can define properties and methods
public string Name { get; set; }
// This method handles the form submission
public void OnPost()
{
Name = Request.Form["name"];
}
}
@page
:指示这是一个 Razor Page,可以在 URL 中直接访问。@model IndexModel
:指定页面使用的模型类。ViewData["Title"]
:设置页面的标题。<form method="post">
:创建一个表单,使用 POST 方法提交数据到服务器。@if (!string.IsNullOrEmpty(Model.Name))
:检查模型中的 Name
属性是否为空,如果不为空则显示欢迎信息。@code { ... }
:定义代码块,在这里可以声明属性和方法。OnPost
方法用于处理表单提交事件。如果你需要更多关于 Razor Pages 的详细信息,请查阅官方文档或相关教程。
下一篇:c# 委托和事件
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站