如何直接访问我的WebGrid中的项目

时间:2015-01-22 03:04:34

标签: asp.net razor webgrid razorengine

我的剃刀视图中有以下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>`

由于

1 个答案:

答案 0 :(得分:1)

您无法访问List中的对象属性&lt;&gt;没有具体说明你想从列表中找到哪个对象。 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>

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