5:显示完整名称
我想从视图顶部的计算属性中显示FullName。 当我查看Autos日志时,我可以看到FullName属性。 但是,Html帮助器中使用的lambda表达式仅显示属性名称,而不显示数据。 有什么方法可以显示日志似乎可用的数据吗?
更新:
完整视图:
@model IEnumerable<SlamJammersData.Model.Attendances>
@{
ViewBag.Title = "Rosters";
}
<div class="container container-fluid">
<div class="panel panel-info">
<div class="panel-body">
<div class="container">
@using (Html.BeginForm("Index", "Attendances", FormMethod.Get))
{
<div class="row">
<div class="col-md-4">
<p>
<h3>Attendances for player: @Html.DisplayNameFor(model => model.FullName) </h3>
</p>
</div>
<div class="col-md-4">
<p>
<h4>Attendances Recorded: @Model.Count() </h4>
</p>
</div>
</div>
}
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-2">
<p>
@Html.DisplayNameFor(model => model.Type)
</p>
</div>
<div class="col-md-2">
<p>
@Html.DisplayNameFor(model => model.AttendanceDate)
</p>
</div>
<div class="col-md-2">
<p>
@Html.DisplayNameFor(model => model.Comments)
</p>
</div>
</div>
@foreach (var item in Model)
{
<div class="row table-striped">
<div class="col-md-2">
<p>
@Html.DisplayFor(modelItem => item.Type)
</p>
</div>
<div class="col-md-2">
<p>
@Html.DisplayFor(modelItem => item.AttendanceDate)
</p>
</div>
<div class="col-md-2">
<p>
@Html.DisplayFor(modelItem => item.Comments)
</p>
</div>
<div class="col-md-1">
<p>
<input class="btn btn-xs btn-info" type="button" value="Details" onclick=" location.href = '@Url.Action("Details", "Attendances", new {id = item.Id}, null)' "/>
</p>
</div>
<div class="col-md-1">
<p>
<input class="btn btn-xs btn-info" type="button" value="Edit" onclick=" location.href = '@Url.Action("Edit", "Attendances", new {id = item.Id}, null)' "/>
</p>
</div>
<div class="col-md-2">
<p>
<input class="btn btn-xs btn-success" type="button" value="Add New Attendance" onclick=" location.href = '@Url.Action("Create", "Attendances", new {enrollments_Attendance = item.Enrollments_Attendance}, null)' "/>
</p>
</div>
<div class="col-md-1">
<p>
<input class="btn btn-xs btn-danger" type="button" value="Delete" onclick=" location.href='@Url.Action("Delete", "Attendances", new {id = item.Id}, null)' " />
</p>
</div>
</div>
}
</div>
控制器:
public ActionResult Index(int? Id )
{
//Get the rosters and their attendance records
IQueryable<Attendances> attendances = db.Attendances
.Where(a => a.Enrollments_Attendance == Id)
.Include(e => e.Enrollments.Individuals)
.OrderByDescending(a => a.AttendanceDate);
ViewBag.EnrollmentId = Id;
var sql = attendances.ToString();
return View(attendances.ToList());
}
该模型:
namespace SlamJammersData.Model
{
public class Attendances
{
public int Id { get; set; }
[Required]
[StringLength(15)]
public string Type { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]
public DateTime AttendanceDate { get; set; }
[StringLength(255)]
public string Comments { get; set; }
[Display(Name="Enrollments")]
public int Enrollments_Attendance { get; set; }
public virtual Enrollments Enrollments { get; set; }
[NotMapped]
public string FullName
{
get
{
var player = Enrollments.Individuals.FirstName + " " + Enrollments.Individuals.LastName;
return player;
}
}
}
}
我不确定它是否是IEnumerable或不是,但这里是调试器中显示的内容:调试器的自动日志
由于你的模型是IEnumerable<SlamJammersData.Model.Attendances>
和FullName
是Attendances
一个属性,所以你不能像使用你想要做的那样在foreach
块之外使用FullName
属性。 但是,假设FullName
的值始终相同,则可以使用ViewBag
。 改变你的控制器方法如下
public ActionResult Index(int? Id )
{
//Get the rosters and their attendance records
IQueryable<Attendances> attendances = db.Attendances
.Where(a => a.Enrollments_Attendance == Id)
.Include(e => e.Enrollments.Individuals)
.OrderByDescending(a => a.AttendanceDate);
ViewBag.EnrollmentId = Id;
ViewBag.FullName = ...; // logic to get FullName here
var sql = attendances.ToString();
return View(attendances.ToList());
}
并在您的视图中显示全名,如下所示
@using (Html.BeginForm("Index", "Attendances", FormMethod.Get))
{
<div class="row">
<div class="col-md-4">
<p>
<h3>Attendances for player: @ViewBag.FullName </h3>
</p>
</div>
<div class="col-md-4">
<p>
<h4>Attendances Recorded: @Model.Count() </h4>
</p>
</div>
</div>
}
我从ASP.NET论坛获得了关于此主题的帮助。 所需的帮手代码行是:
<h3>Attendances for player: @Html.DisplayName(Model.First().FullName) </h3>
感谢所有看过这个问题的人。
更新:我发现了一个不同的代码行,看起来更安全一些。
@Html.DisplayFor(m => m.ElementAt(0).FullName)
更新:我发现Marko评论说,如果记录计数为0,则会引发异常。因此,如果没有要显示的记录,我会在控制器中使用try-catch来显示更友好的消息。
public ActionResult Index(int? Id)
{
//Get the rosters and their attendance records
IQueryable<Attendances> attendances = db.Attendances
.Where(a => a.Enrollments_Attendance == Id)
.Include(e => e.Enrollments.Individuals)
.OrderByDescending(a => a.AttendanceDate);
ViewBag.EnrollmentId = Id;
try
{
ViewBag.FullName = attendances.First().FullName;
}
catch (InvalidOperationException ex)
{
ViewBag.Exception = ex;
ViewBag.FullName = "No Attendances to display for the selected player";
}
return View(attendances.ToList());
}
链接地址: http://www.djcxy.com/p/19087.html
上一篇: 5: Show Full Names
下一篇: Get Label for Expresssion Func<T, U> Only Works for String Type