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

发布于:2024-04-16 ⋅ 阅读:(191) ⋅ 点赞:(0)

 

目录

 

效果图

实现过程 

1创建数据库

 2创建项目文件

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

​编辑 注意这里要写Home​编辑

 创建成功

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

4创建DAL

5创建BLL 

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

​编辑 7HomeController.cs代码

8Index.cshtml代码


效果图

实现过程 

1创建数据库

 2创建项目文件

 

 

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

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

4创建DAL

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 

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代码

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代码

@{
    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>


网站公告

今日签到

点亮在社区的每一天
去签到