View中的多个ViewModel

时间:2012-12-07 19:43:17

标签: asp.net-mvc-3 grid viewmodel kendo-ui

我希望在一个视图中显示两个带有不同信息(模型)的Telerik Kendo网格,我不知道该怎么做。

型号:

using System.Collections.Generic;

namespace KendoUIMvcApplication1.Models
{
    public class Car
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Truck
    {
        public int Id { get; set; }
         public string Name { get; set; }
    }

    public class Vehicles
    {
        public IEnumerable<Car> CarCol { get; set; }
        public IEnumerable<Truck> TruckCol { get; set; }
    }
}

Controller(创建一些测试对象):

using System.Collections.Generic;
using System.Web.Mvc;
using KendoUIMvcApplication1.Models;

namespace KendoUIMvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var carList = new List<Car>();
            var truckList = new List<Truck>();
            var vehCol = new Vehicles();

            var car1 = new Car
            {
                Id = 1, 
                Name = "Passat"
            };
            var car2 = new Car
            {
                Id = 2,
                Name = "Passat"
            };
            carList.Add(car1);
            carList.Add(car2);

            var truck1 = new Truck
            {
                Id = 1,
                Name = "S10"
            };
            var truck2 = new Truck
            {
                Id = 1,
                Name = "Blazer"
            };
            truckList.Add(truck1);
            truckList.Add(truck2);

            vehCol.CarCol = carList;
            vehCol.TruckCol = truckList;

            return View(vehCol);
        }
    }
}

查看(这里我试图显示两个网格。第一个用于汽车,第二个用于卡车):

@using KendoUIMvcApplication1.Models

@model IEnumerable<Vehicles>

@*// Car Grid*@
@(Html.Kendo().Grid(Model)
    .Name("Grid")
    .Columns(columns => {
        columns.Bound(c => c.Id);
        columns.Bound(c => c.Name);
    })
)

<div></div>

@*// Truck Grid*@
@(Html.Kendo().Grid(Model))
    .Name("Grid")
    .Columns(columns => {
        columns.Bound(t => t.Id);
        columns.Bound(t => t.Name);
    })
)

1 个答案:

答案 0 :(得分:2)

您的模型应为Vehicles类型:

@model Vehicles

然后你可以访问这两个集合:

@Html.Kendo().Grid(Model.CarCol)
...

@Html.Kendo().Grid(Model.TruckCol)
...