欢迎各位兄弟 发布技术文章
这里的技术是共享的
使用工具 :Visual Studio 2015 , SQL server
(1)当我们打开 Visual Studio 2015后 ,在首页点击新建项目
(2)这时会有个弹窗,我们选择Empty,勾选MVC 后点击确定
(3)点击确定后进行首页 ,我们在右边找到 ”解决方案资源管理器“ ,如果没有就在单击视图,点击解决方案资源管理器
(4)点击解决方案资源管理器后,我们在Controllers里添加一个控制器
注 :Controllers文件夹是用来放置控制器的文件夹
(5)选择 ” MVC S 控制器 - 空 “ ,点击添加
修改名称
(6)选中 Index() ,点击鼠标右键添加视图
使用布局页不用的打✓,点击添加,一个视图就添加成功了。
成功添加视图后,我们就来进行下一步了。
打开 Visual Studio 2015 后找到 Models(Modees :数据库里面的表会映射到这给文件夹里面来。)
(1)Models ———》添加 ———》 新建项
(2)点击 数据 ———》ADO.NET实体数据模型
(3)点击 来自数据库的 EF 设计器——》确定
(4)点击新建连接 ——》填写服务器名称 ——》登录———》 选择数据库名称——》确定
(5)获取成功
(1)添加一个实例化,实体模型
- public class LinqSelectController : Controller
- {
- // GET: LinqSelect
- //实例化 实体模型
- Models.TestDBEntities myModel = new Models.TestDBEntities();
- public ActionResult Index()
- {
- return View();
- }
实例化 :在面向对象的编程中,通常把用类创建对象的过程称为实例化。
类名 对象名 = new 类名(参数1,参数2...参数n);
如 Date date=new Date();就是用日期类创建了一个日期的对象,就叫对象的实例化。
多数语言中,实例化一个对象 就是为对象开辟内存空间,或者是不用声明,直接使用
new 构造函数名(),建立一个临时对象。
(2)查询表格数据
- /// <summary>
- /// 查询表格数据
- /// </summary>
- /// <returns></returns>
- public ActionResult SelectStudent()
- {
- var student = from tbStudent in myModel.SYS_Student
- select tbStudent;
- return Json(student, JsonRequestBehavior.AllowGet);
- }
(3)在index视图里搭建一个表格样式
HTML部分
- <div class="container">
- <table id="tbStudent" class="table table-bordered mt-5">
- <thead>
- <tr>
- <th>序号</th>
- <th>姓名</th>
- <th>性别</th>
- <th>身份证号码</th>
- <th>电话号码</th>
- <th>地址</th>
- </tr>
- </thead>
- <tbody>
- </tbody>
- </table>
- </div>
JS部分
- $(document).ready(function () {
- $.get("/LinqSelect/SelectStudent", function (jsonData) {
- console.log(jsonData);
- var str = "";
- for (var i = 0; i < jsonData.length; i++) {
- str += '<tr><td>' + (i + 1) +
- '</td><td>' + jsonData[i].StudentName +
- '</td><td>' + jsonData[i].StudentSex +
- '</td><td>' + jsonData[i].StudentIDNum +
- '</td><td>' + jsonData[i].StudentTelphome +
- '</td><td>' + jsonData[i].StudentAddress + '</td></tr>';
- console.log(jsonData[i])
- $("#tbStudent tbody").append(str);
- }
- })
- })
注 :布局使用了Bootstrap框架。
结果:
来自 https://blog.csdn.net/HG13542386794/article/details/117434993