将列表行转换为c#中的列

时间:2017-04-28 16:52:13

标签: c# asp.net-mvc generics

我有一份数据清单。我需要将列表行转换为列。我搜索了很多,但找不到解决方案。

我的行动方法:

qs = self.mixup_querysets(qs)

模特课程:

public ActionResult Index()
{
    var obj = JsonConvert.DeserializeObject<XLDataManager>(new DataUploadService().GetDataUpload(1, 1));
    XLDataManager excelData = (XLDataManager)obj;
    List<Design> listData = new List<Design>() 
    { 
        excelData.CurrentPlan,
        excelData.Design1,
        excelData.Design2,
        excelData.Design3,
        excelData.Design4
    };

    foreach (var items in ListDesign)
    {

        DesignViewModel objDesignViewModel = new DesignViewModel()
        {
            TotalPMPMCost_29 = items.TotalPMPMCost_29,
            PlanType_30 = items.PlanType_30,
            ServicesCoveredDeductible_31 = items.ServicesCoveredDeductible_31,
            ContributionType_32 = items.ContributionType_32,
            ContributionAmount_33 = items.ContributionAmount_33,
            MedicalDeductible_36 = items.MedicalDeductible_36,
            MedicalCoinsurance_37 = items.MedicalCoinsurance_37,
            MedicalOutofPocketMax_38 = items.MedicalOutofPocketMax_38

        };

        DesignViewModelList.Add(objDesignViewModel);
    }

    return View(DesignViewModelList);
}

查看:

public class DesignViewModel
    {
        public float TotalPMPMCost_29 { get; set; }
        public string PlanType_30 { get; set; }
        public string ServicesCoveredDeductible_31 { get; set; }
        public string ContributionType_32 { get; set; }
        public float ContributionAmount_33 { get; set; }
        public float MedicalDeductible_36 { get; set; }
        public float MedicalCoinsurance_37 { get; set; }
        public float MedicalOutofPocketMax_38 { get; set; }

    }

我只需要显示如下数据:

@model IEnumerable<HealthAppDesign.ViewModel.DesignViewModel> 
@{

    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.TotalPMPMCost_29)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PlanType_30)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ServicesCoveredDeductible_31)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ContributionType_32)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ContributionAmount_33)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.MedicalDeductible_36)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.MedicalCoinsurance_37)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.MedicalOutofPocketMax_38)
        </th>

    </tr>

@foreach
    (var item in Model) {
    <tr>

        <td>
            @Html.DisplayFor(modelItem => item.TotalPMPMCost_29)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PlanType_30)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ServicesCoveredDeductible_31)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ContributionType_32)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ContributionAmount_33)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MedicalDeductible_36)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MedicalCoinsurance_37)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MedicalOutofPocketMax_38)
        </td>

    </tr>
}

</table>

目前它将行显示为列..

我们将不胜感激。如果需要更多代码,请告诉我。

由于

1 个答案:

答案 0 :(得分:2)

您需要遍历每一行的视图模型列表:

<table>
    <thead>
        <tr>
            <th><!-- Empty column for labels --></th> 
            @foreach (var item in Model) {
                <th>@Html.DisplayNameFor(model => model.Name)</th>
            }
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Total PMPM Cost</td>
            @foreach (var item in Model) {
                <td>@Html.DisplayFor(modelItem => item.TotalPMPMCost_29)</td>
            }
        </tr>
        <tr>
            <td>Plan Type</td>
            @foreach (var item in Model) {
                <td>@Html.DisplayFor(modelItem => item. PlanType_30)</td>
            }
        </tr>
        <!-- Repeat for all model properties -->
    </tbody>
</table>