zoukankan      html  css  js  c++  java
  • MVC EF 导航属性

    @model IQueryable<EFExam.Models.CategoryProductViewModel>
    @{
        Layout = null;
    }


    <!DOCTYPE html>


    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div>
            <table>
                <tr>
                    <th>CategoryName</th>
                    <th>ProductName</th>
                </tr>
                @foreach (EFExam.Models.CategoryProductViewModel cp in Model)
                {
                    <tr>
                        <td>@cp.NCategoryName</td><td>@cp.NProductName</td>
                    </tr>
                }
            </table>
        </div>
    </body>
    </html>

    using EFExam.Models;
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;


    namespace EFExam.Controllers
    {
        public class CategoryController : Controller
        {
            //
            // GET: /Category/


            public ActionResult Index()
            {
                DbContext context = new StoreContext();
                //var list = from category in context.Set<Category>()
                //           join product in context.Set<Product>()
                //           on category.CategoryID equals product.CategoryID
                //           select category;


                //导航属性 第一种方法1对多 多from语句
                //var list = from category in context.Set<Category>()
                //           from product in category.Products
                //           select new CategoryProductViewModel
                //           {
                //               NCategoryName = category.CategoryName,
                //               NProductName = product.ModelName
                //           };


                //导航属性 第二种方法多对1 
                var list = from product in context.Set<Product>()
                           select new CategoryProductViewModel()
                           {
                               NCategoryName = product.Category.CategoryName,
                               NProductName = product.ModelName
                           };


                return View(list);
            }


        }
    }

  • 相关阅读:
    PHP正则表达式
    PHP日期时间处理
    好文摘录
    Unix时间戳与C# DateTime时间类型互换
    dedecms表结构分析
    css默认值汇总
    jQuery offset,position,offsetParent,scrollLeft,scrollTop html控件定位 css position
    html中label宽度设置、非替换元素和替换元素
    css position, display, float 内联元素、块级元素
    CSS技巧(一):清除浮动
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434741.html
Copyright © 2011-2022 走看看