MVC视图,表循环查询

时间:2015-12-02 07:50:43

标签: c# asp.net-mvc loops view html-table

enter image description here

这就是我正在使用静态数据

在表内实现此视图

静态代码:

   <table class="table table-bordered">
                <thead class="bordered-darkorange bg-blue">

                    <tr>
                        <th class="bg-blue">
                            #
                        </th>
                        <th class="bg-blue">
                            Scheme
                        </th>
                        <th class="bg-blue">
                            Card Type
                        </th>
                        <th class="bg-blue">
                            Commodity
                        </th>
                        <th class="bg-blue">
                            Qty(Kr/Ltr)
                        </th>
                        <th class="bg-blue">
                            Type
                        </th>
                        <th class="bg-blue">
                            Member Count
                        </th>
                    </tr>

                </thead>
                <tbody>

                    @for (int i = 0; i < 4; i++)
                    {
                        <tr>
                            <td>
                                1
                            </td>
                            @if (i == 0)
                            {
                                <td rowspan="4">
                                    Text
                                </td>
                            }
                            @if (i == 0 || i == 2)
                            {
                                <td rowspan="2">
                                    Text
                                </td>
                            }
                            <td>
                                Text
                            </td>
                            <td>
                                <input type="text" class="form-control" style="width: 85%;" maxlength="100" id="txtType" />
                            </td>

                            <td>
                                <select name="ddlDistrict" id="@("CardType"+i)" class="CardType form-control">
                                    <option selected="selected" value="">--Select--</option>
                                    <option value="1">Card Based</option>
                                    <option value="2">Member Based</option>
                                </select>
                            </td>
                            <td>
                                <select name="ddlDistrict" id="@("MemberCount"+i)" class="MemberCount form-control ">
                                    <option selected="selected" value="">--Select--</option>
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                                    <option value="4">4</option>
                                    <option value="5">5</option>
                                    <option value="6">6</option>
                                    <option value="7">7</option>
                                    <option value="8">More Than 7</option>
                                </select>
                                <br class="txtMemberCount" id="@("brMemberCount"+i)">
                                <input type="text" class="txtMemberCount form-control" style="width: 85%;" maxlength="100" id="@("txtMemberCount"+i)" />
                            </td>
                        </tr>
                    }

                    @for (int i = 4; i < 8; i++)
                    {
                        <tr>
                            <td>
                                1
                            </td>
                            @if (i == 4)
                            {
                                <td rowspan="4">
                                    Text
                                </td>
                            }
                            @if (i == 4 || i == 6)
                            {
                                <td rowspan="2">
                                    Text
                                </td>
                            }
                            <td>
                                Text
                            </td>
                            <td>
                                <input type="text" class="form-control" style="width: 85%;" maxlength="100" id="txtType" />
                            </td>

                            <td>
                                <select name="ddlDistrict" id="@("CardType"+i)" class="CardType form-control">
                                    <option selected="selected" value="">--Select--</option>
                                    <option value="1">Card Based</option>
                                    <option value="2">Member Based</option>
                                </select>
                            </td>
                            <td>
                                <select name="ddlDistrict" id="@("MemberCount"+i)" class="MemberCount form-control ">
                                    <option selected="selected" value="">--Select--</option>
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                                    <option value="4">4</option>
                                    <option value="5">5</option>
                                    <option value="6">6</option>
                                    <option value="7">7</option>
                                    <option value="8">More Than 7</option>
                                </select>
                                <br class="txtMemberCount" id="@("brMemberCount"+i)">
                                <input type="text" class="txtMemberCount form-control" style="width: 85%;" maxlength="100" id="@("txtMemberCount"+i)" />
                            </td>
                        </tr>
                    }

                </tbody>
            </table>

但是在动态数据循环的情况下,由于计数不知道,循环不起作用。

表格背后的基本逻辑Commodity取决于CardTypeCardType取决于Scheme

平均1方案可能有多种卡类型,1种卡类型可以有多种商品

更新

我有3个列表List<SchemeMasters>List<RationCards>List<TagCards>

控制器:

var schemename = (from m in db.SchemeMasters select m).ToList();
        ViewBag.schmname = schemename;

        var cardtypcount = (from t in db.RationCards select t).ToList();
        ViewBag.cardtypcount = cardtypcount;

        var commodity = (from x in db.TagCards select x).ToList();
        ViewBag.commodity = commodity;

1 个答案:

答案 0 :(得分:0)

根据我从您的解释中理解,您试图通过未知数量的元素循环,在这种情况下,为什么不尝试foreach。