【问题标题】:Is there any other way to get the count of records in the list<classmodel> in .cshtml?有没有其他方法可以获取 .cshtml 中 list<classmodel> 中的记录数?
【发布时间】:2019-10-04 13:59:15
【问题描述】:

我是 MVC、Kendo 网格和 Jquery 的新手,对我来说似乎很棘手,因为我试图获取模型绑定到网格的计数以执行条件检查,即显示或隐藏 div 元素和按钮。

将我的数据绑定到字段集中的 div 元素。

当我尝试从网格数据源中获取计数时,我可以得到它,但我想像 @model.count 一样获取计数?但我不能。 仅供参考,我在控制台中调试时无法获取模型值。 绑定方式,即从顶部的部分视图@model列表。 有没有其他方法可以获取列表中的记录数?

代码

<fieldset>
   <div class="title" id="msgduplicate">This may be a duplicate Contact. Does this Contact match any of 
        the existing Contacts, below?</div>
   <div class="title" id="msgnoduplicate">No duplicate record found, click Continue to Save the 
        Contact.</div>

     @(Html.SecureGrid<ContactViewModel>()
                                  .Name("DuplicateContactGrid")
                                  .Columns(columns =>
                                  {
                                      columns.Bound(e => e.ContactUid).Hidden(true);
                                      columns.Bound(e => e.ContactRelUid).Hidden(true);
                                      columns.Bound(e => e.ContactEntity).Hidden(true);
                                      columns.Bound(e => e.ResourceUid).Hidden(true);
                                      columns.Bound(e => e.ManagingOrgUid).Hidden(true);
                                      columns.Bound(e => e.FirstName).Width(100)
                                        .HtmlAttributes(new { title = "FirstName" });
                                      columns.Bound(e => e.LastName).Width(100)
                                        .HtmlAttributes(new { title = "LastName" });
                                      columns.Bound(e => e.PhoneNumber).Width(100)
                                        .HtmlAttributes(new { title = "PhoneNumber" });
                                      columns.Bound(e => e.FaxNumber).Width(100)
                                        .HtmlAttributes(new { title = "FaxNumber" });
                                      columns.Bound(e => e.RoleNames).Width(100)
                                        .HtmlAttributes(new { title = "RoleNames " });
                                      columns.Bound(e => e.Notes).Width(150)
                                        .HtmlAttributes(new { title = "Notes" });
                                  })
                                  .Resizable(resize => resize.Columns(true))
                                  .Pageable()
                                  .Navigatable()
                                  .Scrollable(scroll => scroll.Endless(true))
                                  .BindTo(Model)
                                   .Events(e => 
                                      e.Change("selectDuplicateContact").DataBound("setPasGridItems"))
                                  .Selectable(s => s.Mode(GridSelectionMode.Single))
                                  .DataSource(dataSource => dataSource.Server().Model(model => 
                                   model.Id(gd => gd.ContactUid))))
</fieldset>

function () {
            debugger;
            var grid = $("#DuplicateContactGrid").data("kendoGrid");
            var count = grid.dataSource.total();
            if (count > 0) {
                debugger;
                $("#submit").hide();
                $("#msgnoduplicate").hide();
                $("#msgduplicate").show();
            } else {
                $("#submit").show();
                $("#msgnoduplicate").show();
                $("#msgduplicate").hide();

            }
        });

    </script>

【问题讨论】:

    标签: jquery razor model-view-controller kendo-ui grid


    【解决方案1】:

    我从您的问题中了解到,您需要在 javascript 代码中访问 mvc 模型。下面是参考。在您的脚本部分使用下面提到的代码。在此之后,您可以从控制台访问模型属性并能够获取 counts@section script { var model = @Html.Raw(Json.Encode(Model)); //For javascript object }

    【讨论】:

    • 感谢您的时间,我正在尝试进行计数并在 UI 中相应地显示一个按钮,我正在处理来自上一个网格的计数。
    【解决方案2】:

    要在 javascript 中简单地访问模型(这是一个集合)中的项目数,您可以使用:

    var count = @Html.Raw(Model.Count());
    

    请注意,这是在呈现视图时确定的,因此这将无法正确处理更改的行数。如果这是一个要求,您应该继续直接从网格中获取计数并根据需要调用您的函数。

    【讨论】:

    • 谢谢,我已经用另一种方式处理了。有时获取网格数可能是错误的。
    猜你喜欢
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 2011-05-31
    相关资源
    最近更新 更多