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>
相关推荐
IUGEI22 分钟前
synchronized的工作机制是怎样的?深入解析synchronized底层原理
java·开发语言·后端·c#
间彧36 分钟前
GraalVM Native Image:跨平台能力与编译模式深度解析
后端
间彧41 分钟前
GraalVM Native Image 与传统 JVM 内存管理:云原生时代的技术选型指南
后端
r***12381 小时前
SpringBoot最佳实践之 - 使用AOP记录操作日志
java·spring boot·后端
b***74881 小时前
前端GraphQL案例
前端·后端·graphql
LSL666_1 小时前
SpringBoot自动配置类
java·spring boot·后端·自动配置类
q***78372 小时前
Spring Boot 3.X:Unable to connect to Redis错误记录
spring boot·redis·后端
t***26592 小时前
SpringBoot + vue 管理系统
vue.js·spring boot·后端
qq_12498707533 小时前
基于springboot的疾病预防系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
q***2514 小时前
Spring Boot 集成 Kettle
java·spring boot·后端