ASP.NET MVC Webgrid在单行中显示多个列

时间:2014-06-01 05:15:14

标签: asp.net-mvc webgrid

我有一个Asp.net MVC网格。 我的问题是我需要在一行中显示多个列。 例如:

姓名日期赔偿
Id USD - 99999
等级INR - 99999

上面的布局是网格中的一行。

所有列(名称,ID,等级,Curency1,Amount1,Currency2,Amount2)在单个记录中可用作单独的列。这里Currency1表示USD,而Currency2表示INR。

任何想法如何做到这一点。我使用的是强类型模型和EF6。

1 个答案:

答案 0 :(得分:0)

我认为最好的方法是为每个多面列创建一个单独的“类型”和模型,然后尝试在webgrid中显示这种类型(我在后面部分显示了这种类型)例子)。

例如:

创建一个名为CompensationColumn的新“类型”(或“列”)类:

...
using System.Web.Mvc;

namespace yourproject.Columns  // I put this in its own namespace/folder - you don't have to
{
    public class CompensationColumn
    {
        public string Currency1 { get; set; }
        public int Amount1 { get; set; }
        public string Currency2 { get; set; }
        public int Amount2 { get; set; }

        public CompensationColumn(string currency_1, int amount_1, string currency_2, int amount_2)
        {
            Currency1 = currency_1;
            Amount1 = amount_1;
            Currency2 = currency_2;
            Amount2 = amount_2;
        }
    }
}

然后在yourproject / Shared / EditorTemplates文件夹中创建一个名为CompensationColumn.cshtml的文件(如果Shared文件夹不存在,您还可以创建一个view / DisplayTemplates文件夹)。定义此列的外观,就好像它是一个自定义的“类型”(根据自己的喜好修改它):

@model yourproject.Columns.CompensationColumn
@if (Model != null)
{
    @Model.Currency1<text> - </text>@Model.Amount1<text><p/></text>
    @Model.Currency2<text> - </text>@Model.Amount2
}
else
{
}

然后在Models文件夹中,创建一个部分类来扩展当前的EF表模型(文件名无关紧要)。我假设你的桌子是'employee_table'。我也在这个类中为模型添加元数据,因为如果你使用数据库优先设计,它是一个放置它的好地方:

using System.Web.Mvc;
using yourproject.Columns;

namespace yourproject.Models
{
    [MetadataType(typeof(EmployeeModelMetaData))]  // This links the metadata class below
    public partial class employee_table  // This should be the EF class name
    {
        [DisplayName("Compensation")]
        public CompensationColumn Compensation { get; set; }  // Here we add a new field for your row
    }

    public class EmployeeModelMetaData
    {
        // copy your EF class fields here and decorate them with dataannotations.  This is helpful
        // if you are using a database-first design as it won't get overwritten when db changes.
        [DisplayName("Id")]
        public int emp_id { get; set; }
        [DisplayName("Amount")]
        [DisplayFormat(DataFormatString = "{0:c}", ApplyFormatInEditMode = true)]
        public int emp_amount1 { get; set; }
        // etc . . .
    }
}

我在这里做了一些关于数据库优先设计的假设,但是如果需要的话,你应该能够弄清楚如何使它适应代码优先设计。

如果你还需要编辑这个列类型的元素,那么你需要创建一个模型绑定器,但是我不会去那里,因为你只提到了它的显示。

要使显示模板显示在webgrid中,您需要format: webgrid的列。在您的IEnumerable模型视图中(例如您的索引视图):

@{
    var grid = new WebGrid(Model);
    List<WebGridColumn> columns = new List<WebGridColumn>();
    WebGridColumn col = grid.Column(columnName: "Col3", header: "Compensation", format: (item) =>
    {
        yourproject.Columns.CompensationColumn c = item.Compensation; return Html.DisplayFor(model => c); 
    } );
    columns.Add(col);                                                                                                                                
}
@grid.GetHtml(columns: columns)

我改编自FrédéricBlondel的代码here

的最后一段摘录
相关问题