Asp.net 如何直接访问我的WebGrid中的项目
我的razor视图中有以下asp.net webgrid:-Asp.net 如何直接访问我的WebGrid中的项目,asp.net,razor,webgrid,razorengine,Asp.net,Razor,Webgrid,Razorengine,我的razor视图中有以下asp.net webgrid:- @model SkillManagement.Models.PagedList<SkillManagement.Models.Staff> @{ ViewBag.Title = "Index"; } <div class="well"> @using (Html.BeginForm("index", null, FormMethod.Get)) { <div clas
@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>
}
那么,谁能告诉我,我可以采用什么方法直接访问模型项,而不必调用FirstOrDefault
第二个问题。在我的观点顶部,我需要提到完整的项目名称,如下所示:-
Html.DisplayNameFor(model => model.Content.FirstOrDefault().LastName)
@model SkillManagement.Models.PagedList<SkillManagement.Models.Staff>
@model `PagedList<SkillManagement.Models.Staff>`
@model SkillManagement.Models.PagedList
但我的问题是,我怎样才能声明模型,如下所示:-
Html.DisplayNameFor(model => model.Content.FirstOrDefault().LastName)
@model SkillManagement.Models.PagedList<SkillManagement.Models.Staff>
@model `PagedList<SkillManagement.Models.Staff>`
@model`PagedList`
谢谢如果不明确要从列表中选择哪个对象,就无法访问列表中对象的属性。列表可以包含多个对象,要访问任何对象,都需要提到该元素的索引。但是,如果列表中只存储一个元素,那么我建议将列表更改为仅存储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 SkillManagement.Models
@model PagedList<SkillManagement.Models.Staff>
公共类页面列表
{
公共T内容{get;set;}
公共Int32 CurrentPage{get;set;}
公共Int32页面大小{get;set;}
公共整数TotalRecords{get;set;}
公共bool OnlyActive{get;set;}
公共网页
{
获取{return(int)Math.天花((十进制)TotalRecords/PageSize);}
}
}
对于第二个问题,使用@using语句。比如说,
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 SkillManagement.Models
@model PagedList<SkillManagement.Models.Staff>
@使用SkillManagement.Models
@模型页面列表
我希望这能回答您的问题。页面列表肯定会包含多个项目,因为它用于显示网格内的数据列表。。。。那么,如果我处理Html.DisplayNameFor(model=>model.Content.FirstOrDefault().LastName)的方式听起来有效吗?第二个问题,我认为有办法甚至可以从视图顶部删除“@using SkillManagement.Models”,但我记不起来了。因为在default scaffold模板中,您只能提到IEnumerable之类的内容