多个视图视图中的模型



我希望在一个视图中显示两个具有不同信息(模型)的 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; }
    }
}

控制器(创建一些测试对象):

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);
    })
)

您的模型应为 Vehicles 类型:

@model Vehicles

然后,您可以访问两个集合:

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

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

相关内容

  • 没有找到相关文章

最新更新