ASP.NET MVC企业级程序设计 (EF+三层架构+MVP实现查询数据)

目录

效果图

实现过程

1创建数据库

2创建项目文件

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

[​编辑 注意这里要写Home​编辑](#编辑 注意这里要写Home编辑)

创建成功

数据模型创建过程之前作品有具体过程​编辑

4创建DAL

5创建BLL

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

[​编辑 7HomeController.cs代码](#编辑 7HomeController.cs代码)

8Index.cshtml代码


效果图

实现过程

1创建数据库

2创建项目文件

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

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

4创建DAL

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcApplication1.Models;
namespace MvcApplication1.DAL
{
    public class PersonDAL
    {
        public static List<Models.Users> Find(string name) {
            PersonDBEntities db = new PersonDBEntities();
            return db.Users.ToList().Where(x => x.Name.ToString().Contains(name)).ToList();

          
        
        }
    }
}

5创建BLL

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication1.BLL
{
    public class PersonBLL
    {
        public static List<Models.Users> Find(string name)
        {
            return DAL.PersonDAL.Find(name);



        }
    }
}

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

7HomeController.cs代码

cpp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index(string name)
        {
            if (name==null)
            {
                PersonDBEntities db = new PersonDBEntities();
                ViewData["users"] = db.Users.ToList();
            }
            else
            {
                ViewData["users"] = BLL.PersonBLL.Find(name);
            }
            ViewData["xianshi"] = name;
            return View();
        }

    }
}

8Index.cshtml代码

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

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
   <form method="post" action="/Home/Index">
        <label>模糊查找姓名</label> 
       <input type="text" name="name" value="@ViewData["xianshi"]" />
       <input id="Submit1" type="submit"  value="查找"/>
   </form>
              


    <div>
        <table border="1">
            <tr>
                <th>ID</th>
                 <th>姓名</th>
                 <th>年龄</th>
            </tr>
            @{
                foreach (var item in ViewData["users"] as List<MvcApplication1.Models.Users>)
                {
                     <tr>
                <td>@item.ID</td>
                 
                 <td>@item.Name</td>
                         <td>@item.Age</td>
            </tr>
                }
                
                }
        </table>
    </div>
</body>
</html>
相关推荐
whinc11 小时前
Rust技术周刊 2026年第17周
后端·rust
whinc11 小时前
Rust技术周刊 2026年第18周
后端·rust
whinc11 小时前
Rust技术周刊 2026年第16周
后端·rust
jieyucx11 小时前
Go语言深度解剖:Map扩容机制全解析(增量扩容+等量扩容+渐进式迁移)
开发语言·后端·golang·map·扩容策略
王码码203512 小时前
Go语言的内存管理:原理与实战
后端·golang·go·接口
Lee川12 小时前
打字机是怎么炼成的:Chat 流式输出深度解析
前端·后端·面试
Lee川12 小时前
Token 无感刷新与 Logout:前端安全会话管理实战
前端·后端·react.js
舒一笑13 小时前
零后端、零数据库——我做了一个让 10000+ 人成功告白的开源工具
后端·产品·设计师
Java技术小馆14 小时前
如何零成本将各种 AI 编程工具接入免费大模型?
后端
Tutankaaa15 小时前
从10队到50队:知识竞赛软件的高并发场景如何设计?
java·经验分享·后端·spring