【发布时间】: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