zoukankan      html  css  js  c++  java
  • MVC3学习 二 EF查询

    EF操作数据库中的数据非常方便,例如查询:

    OumindBlogEntities db = new OumindBlogEntities();
            public ActionResult Index()
            {
                //db.BlogArticles.Where(d => d.AIsDel == false) 的返回类型为DbQuery ,而DbQuery是延时加载的,也就是说只有当执行query.ToList();才执行查询语句
                //DbQuery<MvcBlog.Models.BlogArticle> query = (db.BlogArticles.Where(d => d.AIsDel == false)) as DbQuery<MvcBlog.Models.BlogArticle>;
                //query.ToList();
    
                List<Models.BlogArticle> list = db.BlogArticles.Where(d => d.AIsDel == false).ToList();
                //Linq 方式查询
                List<Models.BlogArticle> linqList = (from d in db.BlogArticles where d.AIsDel == false select d).ToList();
                //ViewBag方式向前台传递数据
                //ViewBag.DateList = linqList;
                //ViewDate方式向前台传递数据
                ViewData["DateList"] = linqList;
                return View();
    
            }

    因为是将List传到前台页面中,所以在前台页面需要用到foreach循环来输出,代码如下:

    @using MvcBlog.Models;
    
    <table>
        <tr>
            <th>id</th>
            <th>标题</th>
            <th>分类</th>
            <th>状态</th>
            <th>时间</th>
            <th>操作</th>
        </tr>
       @foreach (BlogArticle a in ViewData["DateList"] as List<BlogArticle>)
       {
           <tr>
            <td>@a.AId</td>
            <td>@a.ATitle</td>
            <td>@a.BlogArticleCate.Name</td>
            <td>@a.Enumeration.e_cname</td>
            <td>@a.AAddtime</td>
            <td>
                <a href="javascript:del(@a.AId)">删除</a>
                <a href="/home/modify/@a.AId">修改</a>
                </td>
           </tr>
       }
        
        </table>

    可以直接用@using MvcBlog.Models;引入空间。

    razor真的让开发变的很方便。

  • 相关阅读:
    Notes of Daily Scrum Meeting(12.18)
    Notes of Daily Scrum Meeting(12.17)
    Notes of Daily Scrum Meeting(12.16)
    Notes of Daily Scrum Meeting(12.8)
    Notes of Daily Scrum Meeting(12.5)
    Notes of Daily Scrum Meeting(12.3)
    Notes of Daily Scrum Meeting(11.12)
    Linux中profile、bashrc、bash_profile之间的区别和联系
    Linux GCC编译
    mysql 5.7.16 远程连接
  • 原文地址:https://www.cnblogs.com/y8932809/p/4383006.html
Copyright © 2011-2022 走看看