0

I try to display a list in a Webgrid but the view shows only one column and no matter what I do I can't seems to get it to show the other columns (my model has 3 properties and one of them is complex and contains 4 properties so I expect 6 columns).

View:

@model IEnumerable<AIM.Police.DB.Factor>
@using AIM.Police.DB

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="float-right">

@{
    var grid = new WebGrid(Model);
}

@grid.GetHtml()
</div>

To the View I pass a list of Factor, defined as:

public class Factor  
{
    public Factor()
    {
        this.TraitVector = new TraitVector();
    }

    [Key]
    [Column(Order=0)]
    public FactorTypesEnum FactorType { get; set; }

    [Key]
    [Column(Order = 1)]
    public int ID { get; set; }
    public TraitVector TraitVector { get; set; }      
}

And the FactorTypesEnum and TraitVector are defined as:

public enum FactorTypesEnum
{
    StimulusType = 1,
    OffenseType,
    InterrogationStage,
    InterrogeeStatus,
    ZoneOfRelevance,
    InterrogationExperience,
    InterrogationCount 
}

[ComplexType]
public class TraitVector
{       
    public double AttentionState { get; set; }
    public double AffectState { get; set; }
    public double AttitudeState { get; set; }
    public double AffiliationState { get; set; }
}

Finally here's the Controller's simplest Index action:

    public ActionResult Index(int? id)
    {
        return View(db.Factors.ToList());
    }

The Model contains the expected 47 rows of Factor objects including all properties defined for Factor (FactorType, ID and TraitVector), as read from the DB table and passed to the View by the Action, however in the generated Html only the ID column appears.

It may worth noting that the model, Factor, is defined in a separate DLL and that in the DB all data fields have values (no nulls).

Can anyone figure this out for me please?

4

2 回答 2

0

为了回答我自己的问题,工作是将数据显式绑定到网格,如下所示:

    grid.Bind(Model.Factors, new[] { "FactorType", "ID", 
                         "TraitVector.AttentionState", "TraitVector.AffectState", 
                         "TraitVector.AttitudeState", "TraitVector.AffiliationState"});

由于过去我在 MVC webgrid 中显示其他模型时不需要此显式绑定,因此我只能假设这与FactorType作为枚举类型和/或TraitVector作为复杂类型有关,并且 webgrid 不能自动像对“更简单”类型一样绑定这些字段

于 2013-10-21T06:27:39.633 回答
0

您需要定义列。像这样的东西:

<div id="gridContent">
    @grid.GetHtml(tableStyle: "webGrid",
            headerStyle: "header",
            alternatingRowStyle: "alt",
            selectedRowStyle: "select",
            columns: grid.Columns(
            grid.Column("Id", format: (item) => item.GetSelectLink(item.Id)),
            grid.Column("Name", " Name"),
            grid.Column("Description", "Description", style: "description"),
            grid.Column("Quantity", "Quantity")
     )) 
</div>

有关详细信息,请参阅http://www.codeproject.com/Tips/615776/WebGrid-in-ASP-NET-MVC4

于 2013-09-16T15:08:29.440 回答