MVC方式显示数据(数据库)
新建实体数据模型

选择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方式显示数据(数据库)的更多相关文章
- ABP 极简入门教程(二 MVC方式显示数据)
增加显示菜单 Sample.Web.MVC项目中找到startup目录打开SampleNavigationProvider.cs,根据现有内容添加以下内容 .AddItem( new MenuItem ...
- MVC方式显示数据(手动添加数据)
Model添加类 Customers using System; using System.Collections.Generic; using System.Linq; using System.W ...
- MVC bootstrap-table显示数据时显示No matching records found
问题:bootstrap-table加载数据不显示 @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layou ...
- angularjs表格方式显示数据
<table> <tr ng-repeat="x in names"> <td>{{ x.Name }}</td> <td&g ...
- 怎样操作WebAPI接口(显示数据)
就在去年Insus.NET已经写好的一个WebAPI项目,并且发布在IIS中.参考<创建与使用Web API>http://www.cnblogs.com/insus/p/5019088. ...
- Django学习之天气调查实例(2):显示数据表数据
数据表数据添加后,如添加3条用户信息,分别为“aaa”.“bbb”.“ccc”,现在通过代码的方式显示数据表中的数据. 1.在website项目文件夹中创建 userload.py文件,并且写如下代码 ...
- 利用PHP实现登录与注册功能以及使用PHP读取mysql数据库——以表格形式显示数据
登录界面 <body><form action="login1.php" method="post"><div>用户名:&l ...
- MVC 5使用ViewData(对象)显示数据
控制器协调处理好数据之后,是交由视图来显示数据.在控制器与视图交互有一个是ViewData.这次练习,Insus.NET就以它来做实例. 前些时间,Insus.NET实现的练习中,也有从控制器传数据给 ...
- MVC 5使用ViewBag(对象)显示数据
前面Insus.NET有演示使用ViewData来实现控制器与视图的通讯.如果想了解的话,可以从下面两个链接可以查看:<MVC 5使用ViewData(对象)显示数据>http://www ...
随机推荐
- 开始学习Docker啦--容器理论知识(一)
目录 一.容器核心技术 1.容器规范 2.容器 runtime 3.容器管理工具 4.容器定义工具 5.Registry 6.容器 OS 二.说说容器 1.什么是容器 Containers vs. v ...
- 【ARM-Linux开发】Linux查看设备驱动
驱动操作命令: insmod / modprobe 加载驱动 rmmod 卸载驱动 lsmod 查看系统中所有已经被 ...
- 前端入门系列之CSS
CSS (Cascading Style Sheets) 是用来样式化和排版你的网页的 —— 例如更改网页内容的字体.颜色.大小和间距,将内容分割成多列或者加入动画以及别的装饰型效果. CSS是什么 ...
- jqGrid获取选中行id集合
$("#jqGrid").jqGrid("getGridParam","selarrrow");
- Jira内存调整
java的metaspce怎么调大 元空间虚拟机控制元空间的增长.但是有些时候我们想限制其增长,比如通过显式在命令行中设置-XX:MaxMetaspaceSize.默认情况下,-XX:MaxMetas ...
- C#基础知识学习 linq 和拉姆表达式一
两个方法对比 第二种方法 对比学习 拉姆表达 linq 用法
- python __name__ == ‘__main__’详细解释(27)
学习过C语言或者Java语言的盆友应该都知道程序运行必然有主程序入口main函数,而python却不同,即便没有主程序入口,程序一样可以自上而下对代码块依次运行,然后python不少开源项目或者模块中 ...
- 学习笔记:oracle学习二:oracle11g数据库sql*plus命令之数据库交互、设置运行环境
目录 1.SQL*PLUS与数据库的交互 2.设置sql*plus运行环境 2.1 set命令简介 2.2 使用set命令设置运行环境 2.2.1 pagesize变量 2.2.2 NEWPAGE变量 ...
- vue图片点击放大功能
因项目需求(ui框架element-ui),需要实现图片的点击放大,还要能旋转以及上下切换.当时第一反应,element-ui好像没有这样的组件,就想过自己写,但是那个旋转翻页上下切换感觉有点麻烦,不 ...
- python学习-23 函数
函数 1.函数分为:数学定义的函数和编程语言中的函数 例如: - 数学定义的函数:y=2*x+1 - 编程语言的函数: def test(x): x += 1 return x def :定义函数的 ...