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>
相关推荐
q***965831 分钟前
深入解析Spring Boot中的@ConfigurationProperties注解
java·spring boot·后端
日日行不惧千万里1 小时前
MVC、MVP、MVVM详解
mvc
Jamesvalley1 小时前
flask处理所有logging
后端·python·flask
databook1 小时前
别急着转投 Polars!Pandas 3.0 带着“黑科技”杀回来了
后端·python·数据分析
烟袅1 小时前
为什么调用 OpenAI Tools 后,还要再请求一次大模型?——从代码看 LLM 工具调用的本质
后端·python·llm
e***0962 小时前
SpringBoot下获取resources目录下文件的常用方法
java·spring boot·后端
简创AIGC陶先生2 小时前
【剪映小助手源码精讲】09_音频素材管理系统
后端
will_we2 小时前
Spring Boot4正式篇:第二篇 多版本API特性
java·后端
好好研究2 小时前
SpringMVC框架 - 文件上传
java·spring·mvc·idea
v***5653 小时前
Spring Cloud Gateway
android·前端·后端