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

c# mvc

作者:木齵乄语   发布日期:2026-01-27   浏览:33

// Controller 示例代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyMvcApplication.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            // 返回视图
            return View();
        }

        // GET: Home/Details/5
        public ActionResult Details(int id)
        {
            // 根据ID获取详细信息并传递给视图
            ViewBag.Message = "Details for ID: " + id;
            return View();
        }
    }
}

// View 示例代码 (Index.cshtml)
@{
    ViewBag.Title = "Home Page";
}

<h2>Welcome to the Home Page</h2>

<!-- 链接到 Details 页面 -->
<a href="@Url.Action("Details", "Home", new { id = 1 })">Go to Details</a>

// Model 示例代码
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

解释说明

  1. Controller:

    • HomeController 是一个控制器类,继承自 Controller 类。
    • Index 方法是默认的动作方法,返回一个视图 (View())。
    • Details 方法接受一个 id 参数,并将消息传递给视图。
  2. View:

    • Index.cshtml 是一个 Razor 视图文件,用于显示欢迎页面。
    • 使用 @Url.Action 生成链接到 Details 动作的 URL。
  3. Model:

    • Product 类是一个简单的模型类,包含产品的属性(如 Id, Name, Price)。

这些代码展示了 C# MVC 模式的三个核心部分:控制器(Controller)、视图(View)和模型(Model)。

上一篇:c# async await

下一篇:c# 两个日期之间的天数

大家都在看

c# 二进制

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

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

Laravel 中文站