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>
相关推荐
weixin_985432113 小时前
Spring Boot 中的 @ConditionalOnBean 注解详解
java·spring boot·后端
猎人everest4 小时前
快速搭建运行Django第一个应用—投票
后端·python·django
啾啾Fun6 小时前
精粹汇总:大厂编程规范(持续更新)
后端·规范
yt948326 小时前
lua读取请求体
后端·python·flask
IT_10246 小时前
springboot从零入门之接口测试!
java·开发语言·spring boot·后端·spring·lua
汪子熙7 小时前
在 Word 里编写 Visual Basic 调用 DeepSeek API
后端·算法·架构
寻月隐君7 小时前
手把手教你用 Solana Token-2022 创建支持元数据的区块链代币
后端·web3·github
代码丰8 小时前
使用Spring Cloud Stream 模拟生产者消费者group destination的介绍(整合rabbitMQ)
java·分布式·后端·rabbitmq
烛阴8 小时前
Cheerio DOM操作深度指南:轻松玩转HTML元素操作
前端·javascript·后端
Hello.Reader9 小时前
在多云环境透析连接ngx_stream_proxy_protocol_vendor_module
后端·python·flask