如何在Webgrid中的两个表中显示<list>

时间:2017-07-28 21:03:40

标签: c# asp.net-mvc visual-studio webgrid

我想在WebGrid中显示来自两个表(模型)的数据。为了显示单个模型的列表,我执行以下操作:

(型号)

[Key]
    public int id_Art { get; set; }

    public int id_Pro { get; set; }

    [Required]
    [StringLength(1)]
    public string tipo_Pro { get; set; }

    [Required]
    [StringLength(50)]
    public string nombre_Pro { get; set; }

    [Required]
    [StringLength(100)]
    public string descripcion_Pro { get; set; }

    public byte[] imagen_Pro { get; set; }

    [Required]
    public decimal? precio_Pro { get; set; }

    public int? estatus_Pro { get; set; }

(控制器)

public ActionResult VerArticulos(int page = 1, string sort = "nombre_Pro", string sortdir = "asc", string search = "")
    {

        //WebGridArticulos
        int pageSize = 10;
        int totalRecord = 0;
        if (page < 1) page = 1;
        int skip = (page * pageSize) - pageSize;
        var data = cargarArticulos(search, sort, sortdir, skip, pageSize, out totalRecord);
        ViewBag.TotalRows = totalRecord;
        ViewBag.Search = search;

        return View(data);
    }

public List<articulos> cargarArticulos(string search, string sort, string sortdir, int skip, int pageSize, out int totalRecord)
    {
        using (DbModel db = new DbModel())
        {
            var v = (from a in db.articulos

                     where
                        a.tipo_Pro.Contains(search) ||
                        a.nombre_Pro.Contains(search) ||
                        a.descripcion_Pro.Contains(search)
                     select a
                     );
            totalRecord = v.Count();
            v = v.OrderBy(sort + " " + sortdir);
            if (pageSize > 0)
            {
                v = v.Skip(skip).Take(pageSize);
            }
            return v.ToList();
        }
    }

(查看)

@grid.Table(
                    tableStyle: "table table-responsive table-bordered",
                    headerStyle: "webgrid-header",
                    footerStyle: "webgrid-footer",
                    alternatingRowStyle: "webgrid-alternating-row",
                    rowStyle: "webgrid-row-style",
                    columns: grid.Columns( 
                        grid.Column(header: "IMAGEN", format: @<img src="data:image/jpeg;base64,@SIC.Controllers.ArticuloController.ConvertByteArrayToBase64(@item.id_Art)" alt="" height="70" width="85">),
                        grid.Column(columnName: "tipo_Pro", header: "TIPO"),
                        grid.Column(columnName: "nombre_Pro", header: "NOMBRE"),
                        grid.Column(columnName: "descripcion_Pro", header: "DESCRIPCION"),
                        grid.Column(columnName: "precio_Pro", header: "PRECIO"),
                        grid.Column(header: "⇨", format: (item) => Html.ActionLink("Ver", "Ver", new { }))
    )

我想做同样的事情,但是来自两个表的数据。我发现我必须创建一个包含所需数据的viewmodel,但我无法弄清楚如何实现它。谢谢!

EDIT 这就是我在Webgrid上实现模型的方法 (查看)

@model List<SIC.articulos>
@{
    ViewBag.Title = "VerArticulos";
    var grid = new WebGrid(canPage: true, rowsPerPage: 2);
    grid.Bind(source: Model, rowCount: ViewBag.TotalRows, autoSortAndPage: false);
}

1 个答案:

答案 0 :(得分:0)

将Secound模型绑定到第一个模型。让我们看下面的代码

public class Demo1Model
{
    public int Id { get; set; }

    public stringName{ get; set; }

    public List<Demo2Model> D2{ get; set; }}

现在在视图中.......访问Demo1&amp;一次演示2模型。

    @model IEnumerable<Abc.Models.Demo1Model>
@foreach (var item in Model)
                                        {
@item.Name
}
now access list from demo2 model
@foreach(var item in model.D2)//D2 is object of demo2 model
{
@item.xyz//which property access as u want
}