Output multiple collections in single razor table

In my viewmodel, i have 4 IEnumberable properties that i need to output into a table.

Each property is corresponds to a column in the table. If you look closely, some cells contain more than one value. What would be an ideal what to output this in my razor view?

I haven't used helpers etc up to this point with asp.net mvc. This table requires a bit more logic than a straight forward @for loop.

Here is what my view model looks like

public class FactViewModel
{
    public int AssessmentID { get; set; }
    public IEnumerable<MobilityScore> MobilityScores { get; set; }
    public IEnumerable<SocialScore> SocialScores { get; set; }
    public IEnumerable<FunctionScore> FunctionScores { get; set; }
    public IEnumerable<CognitionScore> CognitionScores { get; set; }
}

*Each of these IEnumberable properties have the same field: id, value, label.

在这里输入图像描述


You could add another property to your FactViewModel that combines all your scores and orders them appropriately. You would need each score type to have a common base type for this to happen, for example Score .

public class FactViewModel
{
    public int AssessmentID { get; set; }
    public IEnumerable<MobilityScore> MobilityScores { get; set; }
    public IEnumerable<SocialScore> SocialScores { get; set; }
    public IEnumerable<FunctionScore> FunctionScores { get; set; }
    public IEnumerable<CognitionScore> CognitionScores { get; set; }
    public IENumerable<Score> Scores
    {
        get
        {
            var scores = new List<Score>();
            scores.Add(MobilityScores);
            scores.Add(SocialScores);
            scores.Add(FunctionScores);
            scores.Add(CognitionScores);
            return scores.OrderBy(e => e.Label);
        }
    }
}

This means you could just use a regular @foreach in razor to output to your table. This hides all the data organisation in the model and not muckying up the view.

@foreach (var score in Model.Scores) {
    <tr>
        <td>@score.Id</td>
        <td>@score.Label</td>
        <td>@score.Value</td>
    </td>
}

If you use this approach, make sure you subclass Score

class MobilityScore : Score { }
class SocialScore : Score { }
class FunctionScore : Score { }
class CognitionScore : Score { }

class Score 
{
    public int Id { get; set; }
    public string Label { get; set; }
    public string Value { get; set; }
}

How are each elements in the set related to one another? If you have multiple items for each component of one assessment does it make sense to make a concatenated object of all the items in each set to display that one thing in the cell? How do the items in each set relate to each other?

I noticed you have radio buttons for each set of items in a column for each assessment ID so if one particular element in MobilityScore has to be selected for something it makes it harder to represent the set as one concatenated item.

You could also (maybe) get away with spanning the row height given the count of the max item out of the 4 collections and for each element in each enumerable then iterate over the max count on each collection and add empty td cells where there are none.

链接地址: http://www.djcxy.com/p/59762.html

上一篇: ASP.NET MVC运行时编译器抱怨模型为空时的错误模型类型

下一篇: 在单张剃刀表中输出多个集合