Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# razor

作者:佳凝皓月   发布日期:2025-02-08   浏览:128

@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# taskcompletionsource

下一篇:c# 委托和事件

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c# rtsp

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站