惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
D
Docker
博客园 - 司徒正美
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
U
Unit 42
J
Java Code Geeks
A
About on SuperTechFans
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security Affairs
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
T
Threatpost
H
Hacker News: Front Page
G
Google Developers Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
S
Secure Thoughts
GbyAI
GbyAI
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - SOSOS's BLog

asp.net mvc3.0 在EF Code-First中自定义Model跟数据库中的表名、字段名的对应关系 征懂IOS开发的同仁,联系QQ 755414 Web前端研发工程师 泛型数据生成Excel Unicode To ASCII,改写至Js ExtJs Extender Controls 3.2.0 截图及下载 多余的项目外包,可长期合作.深圳的朋友(已结束,谢谢支持) webservice小解 介绍一种查找网站被上传的恶意文件的方法 2G空间免费使用,只要你有站 08年又快结束了..抱怨下!~ 基础知识要牢固..复习复习,再复习 今天去面试.net开发,感想 淘宝"新版"首页 样式在.net下测试不成功.附解决办法 在b/s开发中经常用到的javaScript技术 学习.net2.0的网站 网站广告不再影响你网站速度的代码 Ajax技术简单入门
[转]视图多表
SOSOS's BLog · 2018-09-13 · via 博客园 - SOSOS's BLog

在mvc3中,默认是一张数据表对应一个model,一个视图 view只显示一个model。

但是有些时候,我们一个视图上可能需要显示多个model的内容,即一个网页可能要展示多张表的信息,那怎么办呢,这时候,ViewModel就能派上用途了。

ViewModel,顾名思义,专为view服务的model,专门为view视图准备的model。

我这里假设有两个张数据表,Article表和Information表,都需要在首页上显示出来,看看下面的步骤:

一、先写出两张表各自对应的model和相应的DbContext文件,这一步比较简单,我就省略了。

二、创建一个类(ViewModel),取名为indexData

 public class IndexData

    {

        public IEnumerable<Information> Information { get; set; }

        public IEnumerable<Article> Article { get; set; }

        public IndexData()

        {

            Entities db = new Entities();

            this.Information = db.Information.ToList();

            this.Article = db.Article.ToList();

        }

    }

三、控制器Controller

    public ActionResult Index()

        {

            IndexData ind = new IndexData();

            return View(ind);

        }

四、view视图

@model IndexData

<div>

   <ul>

   @foreach (var item in Model.Information.Take(8))

   {

      <li>@Html.DisplayFor(m => item.InfoTitle)</li>

   }

    </ul>

</div>

<div>

   <ul>

    @foreach (var item in Model.Article.Take(8))

     {

        <li>@Html.DisplayFor(m => item.ArticleTitle)</li>

      }

   </ul>

</div>

如果Article表又想分成两部分来显示,则可以这样:

@model IndexData

<div>

   <ul>

   @foreach (var item in Modelwww.huashijixun.com?Article.Where(c=>c.type=="news").Take(8))

   {

      <li>@Html.DisplayFor(m => item.InfoTitle)</li>

   }

    </ul>

</div>

<div>

   <ul>

   @foreach (var item in Model.Article.Where(c=>c.type=="story").Take(8))

   {

      <li>@Html.DisplayFor(m => item.InfoTitle)</li>

   }

    </ul>

</div>

我这里的ViewModel里面只涉及到了两张表,实际上更多张表也是一样的。有些门户网站的首页,可能需要显示十几个model,做法完全是一样的。