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>
相关推荐
GetcharZp1 小时前
告别 jq 噩梦!这款 JSON 神器 fx 让你在终端体验“丝滑”的数据操作
后端
小码哥_常2 小时前
告别臃肿!Elasticsearch平替Manticore登场
后端
苍何3 小时前
万字保姆级教程:Hermes+Kimi K2.6 打造7x24h Agent军团
后端
我叫黑大帅3 小时前
为什么map查找时间复杂度是O(1)?
后端·算法·面试
FreeCultureBoy5 小时前
用 phpbrew 管理 php 环境:从安装到多版本切换
后端·php
FreeCultureBoy6 小时前
用 jenv 管理 Java 环境:从安装 JDK 到多版本切换
后端
IT_陈寒6 小时前
Vite的热更新突然失效,原来是因为这个配置
前端·人工智能·后端
考虑考虑6 小时前
SQL语句中的order by可能造成时间重复
数据库·后端·mysql
Pkmer6 小时前
古法编程: 代理模式
后端·设计模式
文心快码BaiduComate7 小时前
Comate搭载Kimi K2.6,长程13h!
前端·后端·程序员