在MVC中将一个模型多表显示数据到一个网格

时间:2016-05-02 12:19:27

标签: c# asp.net-mvc

我在MVC中有一个模型类

public  class Employee
    {
        public int empId { get; set; }
        public string Name { get; set; }    
        public int Age { get; set; }   
        public string Dob { get; set; }       
        public string Email { get; set; }       
        public string Country { get; set; }
        public string State { get; set; }
        public string District { get; set; }
        public string Qualification { get; set; }

    }

但是他们的数据存储在不同的表中(sql server中的3个表),我希望使用实体框架在一个Web Grid(NOT ON TABLE)中显示所有数据 。 我该怎么办?

2 个答案:

答案 0 :(得分:1)

假设您的数据存储在三个表A(a1,a2,a3),B(b1,b2),C(c1,c2)中,然后通过连接所有3个表或根据您的业务逻辑获得结果想要在IEnumerable集合中执行并存储结果。最后在Web网格中提供as数据源。

您可以使用Entity框架获取数据,如下所示

IEnumerable<Employee> employees = (from m in A
                                   join n in B joins m.a1 equals = n.b1 \\join condition
                                   join o in C joins n.b1 equals o.c1 \\put your join condition
                                   select new Employee{
                                   empId=m.a1,
                                   Name = m.a2,
                                   Country = n.b2 //and so on
                                   }).ToList();

   rerurns employees;

现在使用网格绑定员工

答案 1 :(得分:0)

我使用WebGrid绑定了两个表中的数据并显示在单个页面中:

public class HomeController : BaseController
    {
        [HttpGet]
        public async Task<ActionResult> Index()
        {

            string loginUserId = TempData[WebConstants.LoginUserId].ToString();
            string loginUserName = TempData[WebConstants.LoginUserName].ToString();
            string authmode = TempData[WebConstants.AuthenticationMode].ToString();
            HttpResponseMessage response_Cat = null;
            HttpResponseMessage response_Printer = null;
            IEnumerable<Catridge> list_Cat = null;
            IEnumerable<Printer> list_Printer = null;

            if (loginUserId == "1" && authmode == "Admin")
            {
                CustomLogger.Write("Service Call: Get All Catridge: Start");
                response_Cat = await ServiceGet(ClientConfig.ServiceLayerUrl + ClientConfig.GetAllCatridgeUrl);
                CustomLogger.Write("Service Call: Get All Catridge: End");

                var result_Cat = response_Cat.Content.ReadAsStringAsync().Result;
                list_Cat = JsonConvert.DeserializeObject<IEnumerable<Catridge>>(result_Cat).OrderByDescending(s => s.Id);                    


                CustomLogger.Write("Service Call: Get All Printer: Start");
                response_Printer = await ServiceGet(ClientConfig.ServiceLayerUrl + ClientConfig.GetAllPrinterUrl);
                CustomLogger.Write("Service Call: Get All Printer: End");

                var result_Printer = response_Printer.Content.ReadAsStringAsync().Result;
                list_Printer = JsonConvert.DeserializeObject<IEnumerable<Printer>>(result_Printer).OrderByDescending(s => s.Id);                    

                dynamic mymodel = new ExpandoObject();
                mymodel.Catridge = list_Cat;
                mymodel.Printer = list_Printer;
                return View(mymodel);

            }


            else
            {
                CustomLogger.Write("Service Call: GetAllPrinters: Failed|" + response_Printer);
                ModelState.AddModelError(Resources.Resource.MasterError, Resources.Resource.UnableToProcessRequest);
                list_Printer = new List<Printer>();
                return View(list_Printer.AsEnumerable());
            }

        }

    }

现在,我已将控制器结果数据绑定到MVC中的WebGrid:

@model dynamic   

<div class="container">
    <div class="row">
        <h1 class="mainHeading col-md-12"><b>Cartridge List</b></h1>
    </div>  

    <div class="row">
        @{
            WebGrid grid_Cart = new WebGrid(Model.Catridge, canPage: true, rowsPerPage: 5);
            @grid_Cart.GetHtml(
         htmlAttributes: new { id = "GridTable_Cart" },
         tableStyle: "table table-bordered table-striped",

        columns: new[] {
grid_Cart.Column("TeamName",header: "Team Name", canSort:false,style : "gridTableID"),
grid_Cart.Column("PartNo",header: "PartNo", canSort:false,style : "gridTableID"),
grid_Cart.Column("PrinterName",header: "Printer Name", canSort:false,style : "gridTableID"),
grid_Cart.Column("LoginUserName",header: "Requester Name", canSort:false,style : "gridTableID"),
grid_Cart.Column("CatridgeColor",header: "Cartridge Color", canSort:false,style : "gridTableID"),
grid_Cart.Column("CatridgeDrumsColor",header: "DrumsColor", canSort:false,style : "gridTableID"),
grid_Cart.Column("Others",header: "Others", canSort:false,style : "gridTableID"),
grid_Cart.Column("AssigneeName",header: "Assignee", canSort:false,style : "gridTableID"),
grid_Cart.Column("Status",header: "Status", canSort:false,style : "gridTableID")
     })
        }

    </div>
</div>

<div class="container">
    <div class="row">
        <h1 class="mainHeading col-md-12"><b>Printer List</b></h1>
    </div>

    <div class="row">
        @{
            WebGrid grid_printer = new WebGrid(Model.Printer, canPage: true, rowsPerPage: 5);
            @grid_printer.GetHtml(
    htmlAttributes: new { id = "GridTable_Printer" },
    tableStyle: "table table-bordered table-striped",

    columns: new[] {
    grid_printer.Column("TeamName",header: "Team Name", canSort:false,style : "gridTableID"),
    grid_printer.Column("PrinterName",header: "Printer Name", canSort:false,style : "gridTableID"),
    grid_printer.Column("Version",header: "Version", canSort:false,style : "gridTableID"),
    grid_printer.Column("Bundle",header: "Printer Bundle", canSort:false,style : "gridTableID"),
    grid_printer.Column("PrinterFloorNo",header: "FloorNo", canSort:false,style : "gridTableID"),
    grid_printer.Column("PrinterDeskNo",header: "DeskNo", canSort:false,style : "gridTableID"),
    grid_printer.Column("AssigneeName",header: "Assignee", canSort:false,style : "gridTableID"),
    grid_printer.Column("Status",header: "Status", canSort:false,style : "gridTableID")
                })
        }

    </div>
</div>

上面的代码输出如下: enter image description here