新建实体数据模型

选择ADO.NET实体数据模型,名称改为数据库名

因为使用现有数据库,所以选择来自数据库的EF设计器,只演示所以只选择一个表,空模型可后期增加表

选择从数据库更新模型

新建数据库连接

选择EF6.X框架

选择要查询数据的表

选择后的实体数据库设计视图

引用异步、EF、数据模型命名空间

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading.Tasks;
using System.Data.Entity;
using WebApplication1.Models; namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
private NorthwindEntities db = new NorthwindEntities();
public ActionResult Index()
{
return View();
} public ActionResult About()
{
ViewBag.Message = "Your application description page."; return View();
} public ActionResult Contact()
{
ViewBag.Message = "Your contact page."; return View();
}
//异步加载
public async Task<ActionResult> CusAsync()
{
return View("Customers", await db.Customers.ToListAsync());
}
//根据查询条件
public ActionResult Customers()
{
string id = Request.Params["cusid"];
var cus = from c in db.Customers where c.CustomerID == id select c;
return View("Customers", cus);
}
}
}

视图代码

 @model IEnumerable<WebApplication1.Models.Customers>
@{
ViewBag.Title = "Customers";
} <h2>Customers</h2>
<table class="table">
<caption>基本的表格布局</caption>
<thead>
<tr>
<th>CustomerID</th>
<th>CompanyName</th>
<th>ContactName</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelitem => item.CustomerID)</td>
<td>@Html.DisplayFor(modelitem => item.CompanyName)</td>
<td>@Html.DisplayFor(modelitem => item.ContactName)</td>
</tr>
}
</tbody>
</table>

显示异步加载效果

根据查询条件显示效果

MVC方式显示数据(数据库)的更多相关文章

  1. ABP 极简入门教程(二 MVC方式显示数据)

    增加显示菜单 Sample.Web.MVC项目中找到startup目录打开SampleNavigationProvider.cs,根据现有内容添加以下内容 .AddItem( new MenuItem ...

  2. MVC方式显示数据(手动添加数据)

    Model添加类 Customers using System; using System.Collections.Generic; using System.Linq; using System.W ...

  3. MVC bootstrap-table显示数据时显示No matching records found

    问题:bootstrap-table加载数据不显示 @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layou ...

  4. angularjs表格方式显示数据

    <table> <tr ng-repeat="x in names"> <td>{{ x.Name }}</td> <td&g ...

  5. 怎样操作WebAPI接口(显示数据)

    就在去年Insus.NET已经写好的一个WebAPI项目,并且发布在IIS中.参考<创建与使用Web API>http://www.cnblogs.com/insus/p/5019088. ...

  6. Django学习之天气调查实例(2):显示数据表数据

    数据表数据添加后,如添加3条用户信息,分别为“aaa”.“bbb”.“ccc”,现在通过代码的方式显示数据表中的数据. 1.在website项目文件夹中创建 userload.py文件,并且写如下代码 ...

  7. 利用PHP实现登录与注册功能以及使用PHP读取mysql数据库——以表格形式显示数据

    登录界面 <body><form action="login1.php" method="post"><div>用户名:&l ...

  8. MVC 5使用ViewData(对象)显示数据

    控制器协调处理好数据之后,是交由视图来显示数据.在控制器与视图交互有一个是ViewData.这次练习,Insus.NET就以它来做实例. 前些时间,Insus.NET实现的练习中,也有从控制器传数据给 ...

  9. MVC 5使用ViewBag(对象)显示数据

    前面Insus.NET有演示使用ViewData来实现控制器与视图的通讯.如果想了解的话,可以从下面两个链接可以查看:<MVC 5使用ViewData(对象)显示数据>http://www ...

随机推荐

  1. Python - Django - 使用 Bootstrap 样式修改书籍列表

    展示书籍列表: 首先修改原先的 book_list.html 的代码: <!DOCTYPE html> <!-- saved from url=(0042)https://v3.bo ...

  2. Delphi下Treeview控件基于节点编号的访问

    有时我们需要保存和重建treeview控件,本文提供一种方法,通过以树结构节点的编号访问树结构,该控件主要提供的方法如下:      function GetGlobeNumCode(inNode:T ...

  3. Apache配置优化之开启GZip传输

    1.确保apache已经编译的模块里有mod_deflate模块 2.确保apache的配置文件里引入了压缩的模块 3.确保要开启Gzip压缩的虚拟主机配置里有如下配置,并重启apache服务:如果要 ...

  4. Node.js在跑Express的时候有个时候会卡住按一下Ctrl+C又好了的解决办法

    Node.js编写了一个基于Express的Web应用,但是有的时候这个应用会卡死. 后来发现原因是我使用了Windows原生的命令行,会出现这个问题. 也就是说我是在文件夹下面Shift+鼠标右键, ...

  5. 记录一次idea的git导致的macpro风扇狂转问题

    现象 macpro风扇忽然狂转不停:使用软件Macs Fan Control检测如下:cpu温度持续过高(超过50度),风扇当前转速比较高 解决 观察cpu使用情况,发现git占用cpu很高,而且持续 ...

  6. sonar:soanrqube接口api

    背景: jenkins+sonar集成了代码扫描,但是发出的邮件不管项目质量是通过还是失败,每次邮件的标题都是jenkins的构建状态,所以需要获取sonar中该项目的扫描结果. 解决: 在sonar ...

  7. PHP实现无限极分类的两种方式,递归和引用

    面试的时候被问到无限极分类的设计和实现,比较常见的做法是在建表的时候,增加一个PID字段用来区别自己所属的分类 $array = array( array('id' => 1, 'pid' =& ...

  8. CentOS 7 命令

    常用命令 文件与目录操作 命令 解析 cd /home 进入 ‘/home’ 目录 cd .. 返回上一级目录 cd ../.. 返回上两级目录 cd - 返回上次所在目录 cp file1 file ...

  9. kotlin基本数据类型

    通过idea创建kotlin项目: 创建kotlin文件 package com.czhappy.chapter01 var aBoolean:Boolean = true var anInt:Int ...

  10. electron node.js 实现文件拖动读取文件

    css/styles.css .for_file_drop { width: 100%; height: 100px; background-color: blueviolet; } index.ht ...