【问题标题】:How to directly access items inside my WebGrid如何直接访问我的 WebGrid 中的项目
【发布时间】:2015-03-20 18:19:50
【问题描述】:

我的剃刀视图中有以下 asp.net webgrid:-

@model SkillManagement.Models.PagedList<SkillManagement.Models.Staff>

@{
    ViewBag.Title = "Index";
}

<div class="well">
   @using (Html.BeginForm("index", null, FormMethod.Get))
    {
      <div class="row">There are @Model.TotalRecords item.</div>
        <div class="row">
            <div class="col-sm-8">
                <div class="input-group">

                    <input type="text"
                        name="filter"
                        value="@ViewBag.filter"
                        class="form-control"
                        style="display: inline"
                        placeholder="Search by First & Last Name" />
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="submit">Go</button>
                    </span>

                </div>
            </div>
            <div class="pull-right col-lg-1">   
                <a class="btn btn-success" data-modal="" href="/Staff/Create" id="btnCreate">
                     <span class="glyphicon glyphicon-plus"></span>      
                </a>
            </div>
        </div>

        <div style="margin-top:17px;">
            @{
            var grid = new WebGrid(
                        canPage: true,
                        rowsPerPage: Model.PageSize,
                        canSort: true,
                        ajaxUpdateContainerId: "grid");

            grid.Bind(Model.Content, rowCount: Model.TotalRecords, autoSortAndPage: false);
            grid.Pager(WebGridPagerModes.All);

            @grid.GetHtml(htmlAttributes: new { id = "grid" },   // id for ajaxUpdateContainerId parameter
            fillEmptyRows: false,
            tableStyle: "table table-bordered table-hover",
            mode: WebGridPagerModes.All,
            columns: grid.Columns(
              grid.Column("FirstName", Html.DisplayNameFor(model => model.Content.FirstOrDefault().FirstName).ToString()),
              grid.Column("LastName", Html.DisplayNameFor(model => model.Content.FirstOrDefault().LastName).ToString()),
              grid.Column("PrimaryRole", Html.DisplayNameFor(model => model.Content.FirstOrDefault().PrimaryRole).ToString()),
              grid.Column("SecondaryRole", Html.DisplayNameFor(model => model.Content.FirstOrDefault().SecondaryRole).ToString()),
              grid.Column("Skill",canSort:false,format:

                 @<text>
            @foreach (var c in item.SkillLevelStaffs)
{
    @c.Skill.Name;
}


                   </text>),
              grid.Column("Actions",canSort:false,format:

                 @<text>



                   <a data-modal='' href="/Staff/details/@item.StaffID"   id="@item.StaffID" title="Details"> <span class='glyphicon glyphicon-search'> </span> </a>
                  <a data-modal='' href="/Staff/edit/@item.StaffID"   id="@item.StaffID" title="Edit"> <span class='glyphicon glyphicon-edit'> </span> </a>
                   @if(item.ISActive){<a data-modal='' href="/Staff/Deactivate/@item.StaffID"   id="@item.StaffID" title="Deactivate"> <span class='glyphicon glyphicon-alert'> </span> </a>}
            else if(!item.ISActive){<a data-modal='' href="/Staff/delete/@item.StaffID"   id="@item.StaffID" title="Delete"> <span class='glyphicon glyphicon-trash'> </span> </a>}
                </text>
                       )

            ));
            }


        </div>
    }
</div>


<!-- modal placeholder-->
<div id='myModal' class='modal fade in'>
    <div class="modal-dialog">
        <div class="modal-content">
            <div id='myModalContent'></div>
        </div>
    </div>
</div>

@section scripts{

@Scripts.Render("~/bundles/jqueryval") 
<script src="~/Scripts/skillmanagementgrid.js"></script>
}

我定义了以下模型:-

    public class PagedList<T>
    {
        public List<T> Content { get; set; }

        public Int32 CurrentPage { get; set; }
        public Int32 PageSize { get; set; }
        public int TotalRecords { get; set; }
        public bool OnlyActive { get; set; }
        public int TotalPages
        {
            get { return (int)Math.Ceiling((decimal)TotalRecords / PageSize); }
        }
    }
}

现在如我的视图中所示,如果我想引用网格内的项目,我需要使用 FirstOrDefualt,如下所示:-

Html.DisplayNameFor(model => model.Content.FirstOrDefault().LastName)

那么任何人都可以提出我可以遵循哪些方法来直接访问模型项而无需调用 FirstOrDefault 吗?

第二个问题。在我看来,我需要提到完整的项目名称如下:-

 @model SkillManagement.Models.PagedList<SkillManagement.Models.Staff>

但我的问题是我如何才能将模型声明如下:-

 @model `PagedList<SkillManagement.Models.Staff>`

谢谢

【问题讨论】:

    标签: asp.net razor webgrid razorengine


    【解决方案1】:

    如果不具体说明要从列表中获取哪个对象,则无法访问 List 中对象的属性。 List 可以包含多个对象,并且要访问任何对象,都需要提及该元素的索引。但是,如果您只在列表中存储一个元素,那么我建议您将列表更改为仅 T。 例如,

    public class PagedList<T>
    {
        public T Content { get; set; }
    
        public Int32 CurrentPage { get; set; }
        public Int32 PageSize { get; set; }
        public int TotalRecords { get; set; }
        public bool OnlyActive { get; set; }
        public int TotalPages
        {
            get { return (int)Math.Ceiling((decimal)TotalRecords / PageSize); }
        }
    }
    

    对于第二个问题,请使用@using 语句。例如,

    @using SkillManagement.Models
    @model PagedList<SkillManagement.Models.Staff>
    

    我希望这能回答你的问题。

    【讨论】:

    • PagedList 肯定会包含多个项目,因为它用于显示网格内的数据列表....所以如果我这样做的方式是 Html.DisplayNameFor(model => model .Content.FirstOrDefault().LastName) 听起来有效吗?
    • 第二个问题,我认为甚至可以从视图顶部删除“@using SkillManagement.Models”,但我不记得了。因为在默认的脚手架模板上,您只能提到诸如 IEnumerable 之类的内容
    猜你喜欢
    • 2013-06-08
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 2012-11-13
    • 1970-01-01
    相关资源
    最近更新 更多