使用jquery或javascript迭代泛型集合

时间:2014-03-06 10:17:01

标签: jquery asp.net-mvc view generic-collections

我有一个动态创建表结构的视图

<div>
        @if (Model.parameterListForReviewerOne != null)
        {
            foreach (var item in Model.parameterListForReviewerOne)
            {
                if (item.parmID != null && item.parmID != 0)
                {
            <tr class="highlightRed parameterOfReviewer1 bluebgtable-confi">
                <td style="display: none">@Html.HiddenFor(modelItem => item.parmID)
                </td>
                <td align="center" width="25%">
                    @Html.DisplayFor(modelItem => item.ParameterDesc, new { @readonly = "readonly" })
                </td>
                <td align="center" class="one" width="35%">
                    @*@Html.TextBoxFor(modelItem => item.Reviewer1Ratings, new { @class = "ReviewerOneClass", @id = "Ratings1_" + @item.parmID })*@
                    @Html.DropDownListFor(model => item.Reviewer1Ratings, new SelectList(Model.RatingsList, item.Reviewer1Ratings), new { @class = "ReviewerOneClass HideinPrint", @id = "Ratings1_" + @item.parmID })
                    <span class="ShowinPrint" id="forPrintRatings1_@item.parmID">dummy</span> <span style="color: #E80C4D; display: none;" class="hide" id="span_Reviewer1Rating_@item.parmID">
                        Invalid range.</span> <span style="color: #E80C4D; display: none;" class="hide" id="span_Reviewer1RatingRequired_@item.parmID">
                            Required</span>
                </td>
                <td align="center" class="one" height="80px" width="40%">
                    @Html.TextAreaFor(modelItem => item.Reviewer1Comments, new { @class = "ReviewerCommentsClass requiredField disableReviewer1 reviewer1ParaFieldLimit HideinPrint", @id = "Comments1_" + @item.parmID })
                    <div class="ShowinPrint" id="Comments1_@item.parmID">
                        dummy</div>
                    <span style="color: #E80C4D; display: none;" class="hide hideComments1_@item.parmID" id="span_Reviewer1CommentsRequired_@item.parmID">
                        Required</span> <span id="spn_Comments1_@item.parmID" class="classSec3FieldLimit" style="display: none">
                        </span>
                </td>
            </tr>
                }
            }
        }
        <tr class="bluebgtable-confi">
            <td align="center" width="25%">
                Overall Ratings
            </td>
            <td align="center" id="OverallRating1" valign="middle" height="60px" width="35%">
                @* @Html.TextBoxFor(model => model.Reviewer1OverallRating)*@
                @Html.DropDownListFor(model => model.Reviewer1OverallRating, new SelectList(Model.RatingsList), new { @class = "HideinPrint" })
                <span class="ShowinPrint" id="forPrintReviewer1OverallRating">dummy</span> <span
                    style="color: #E80C4D; display: none;" class="hide" id="span_OverAllRatingReviewer1">
                    Invalid range.</span> <span style="color: #E80C4D; display: none;" class="hide" id="span_OverAllRatingReviewer1Required">
                        Required</span>
            </td>
            <td id="OverallRating1" align="center" class="requiredField" height="80px" width="40%">@Html.TextAreaFor(model => model.Reviewer1OverallRatingComments, new { @class = "disableReviewer1 HideinPrint" })
                <div class="ShowinPrint" id="forPrintReviewer1OverallRatingComments">
                    dummy</div>
                <p>@Html.ValidationMessageFor(model => model.Reviewer1OverallRatingComments)</p>
                <span style="color: #E80C4D; display: none;" class="hide hideOverallRating1" id="span_Reviewer1OverallRatingCommentsRequired">
                    Required</span>
            </td>
        </tr>
    </div>

在javascript函数中我希望获得动态生成的parmID如何在javascript或jquery中获取parmID。实际上我想将代码复制到div,这将隐藏我想要获取动态生成的ID。

2 个答案:

答案 0 :(得分:0)

在MVC4中,您可以编写<tr id="@Html.IdFor(modelItem => item.parmID)" ...,然后使用jquery选择这些ID。 或生成一些javascript,即

<script>
 var id = '@Html.IdFor(modelItem => item.parmID)';
</script>

或在tr <tr class='myclass'>上设置属性或类,只需使用$('.myclass').forEach(...)

答案 1 :(得分:0)

首先修改您的tr以存储其parmID。像:

<tr data-parmID="@item.parmID" class="highlightRed parameterOfReviewer1 bluebgtable-confi">
.
.
.
</tr>

你可以迭代它们之后

$("tr").each(function() {
    var parmID = $(this).attr("data-parmID");
    // do your work here
});