无法使用foreach循环检索WebGrid行的值?

时间:2019-05-20 13:12:38

标签: javascript jquery asp.net model-view-controller webgrid

我正在尝试从以下WebGrid检索数据:

@{
                if (Model != null && Model.Count() > 0)
                {
                    var grid = new WebGrid(canPage: true, rowsPerPage: 10);
                    grid.Bind(source: Model, rowCount: 0, autoSortAndPage: false);

                    <div class="row" id="content2">
                        <div class="col-lg-12 d-flex align-items-stretch">
                                     @grid.Table(tableStyle: "table table-responsive table-striped table-bordered",
                                     htmlAttributes: new { id = "tableID" },
                                     columns: grid.Columns(
                                     grid.Column(columnName: "A", header: "A", format:@<text><div class="item1" data-id="@item.KeyA" data-propertyname="KeyA">@item.KeyA</div></text>),
                                     grid.Column("Other", header: "Other", format: @<text>@Html.DropDownList("OtherKey", (IEnumerable<SelectListItem>)ViewBag.OtherKeysList, new { @class = "extra-class" })</text>)))
                        </div>
                        <!--Pagerlist for webgrid-->
                        <div class="col-lg-12 d-flex align-items-stretch">
                            @grid.PagerList(mode: WebGridPagerModes.All, paginationStyle: "pagination pagination-small pagination-right")
                        </div>

                        @Html.Hidden("dir", grid.SortDirection)
                        @Html.Hidden("col", grid.SortColumn)
                    </div>
                }
}

我正在使用以下代码检索“其他”列的值:

$(".extra-class").each(function() {
  console.log($(this).val());
});

但是,当我尝试以相同的方式检索“ A”列的值时(请参见下面的代码),出现了 undefined 错误。

$(".item1").each(function() {
  console.log($(this).val());
});

希望有人可以帮助我!

谢谢!

0 个答案:

没有答案