ASP.NET MVC企业级程序设计 (EF+MVP实现显示数据)

效果图

实现过程

1创建数据库

2创建项目文件

3创建控制器,右键添加,控制器

注意这里要写Home
创建成功
数据模型创建过程之前作品有具体过程

4创建视图,右键添加视图

5HomeController.cs代码

cpp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication3.Models;
namespace WebApplication3.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            PersonDBEntities db=new PersonDBEntities();
            ViewData["users"] = db.Users.ToList();
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

6Index.cshtml代码

html 复制代码
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        <table border="1">
            <tr>
                <th>ID</th>
                <th>姓名</th>
                <th>年龄</th>
            </tr>
            @{ 
                foreach (var item in ViewData["users"] as List<WebApplication3.Models.Users>)
                {
                        <tr>
                            <td>@item.id</td>
                            <td>@item.name</td>
                            <td>@item.age</td>
                        </tr>
                }

            }

        </table>
    </div>
</body>
</html>
相关推荐
fox_lht1 小时前
第十二章 泛型、接口和生命周期
开发语言·后端·rust
ikoala1 小时前
用了几周明基 RD280UG,我终于明白程序员为什么需要一台“专用显示器”
前端·后端·程序员
文心快码BaiduComate1 小时前
Comate搭载DeepSeek-V4
前端·后端
小杍随笔1 小时前
Axum+Leptos全栈集成实战
开发语言·后端·架构·rust
小小小小宇1 小时前
订单超时自动取消方案详解
后端
java1234_小锋1 小时前
SpringBoot可以同时处理多少请求?
java·spring boot·后端
12344521 小时前
网络IO模型
后端·操作系统
用户467245132231 小时前
synchronized的"双重人格":静态与非静态方法锁的惊天差异
后端
胡志辉2 小时前
Nginx CVE‑2026‑42945:隐藏18年高危漏洞被曝光(附解决方案)
前端·后端·nginx
BestHeaker2 小时前
CC Switch 全能使用教程
后端·职场和发展·跳槽·学习方法