我需要在视图中通过逗号显示学生姓名+姓氏。
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
我的观点看起来像,我知道hpw要添加一个列,但我不知道如何向学生展示姓名+ LastName
<table class="table">
<tr>
<th>
@Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm })
</th>
<th>
First Name
</th>
<th>
@Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm })
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstMidName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EnrollmentDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
答案 0 :(得分:1)
您可以尝试添加部分类以将FirstName + Surname显示为Fullname ...不确定这是否有帮助
public partial class Student
{
public string FullName
{
get
{
return string.Join(" ", FirstMidName, LastName);
}
}
}
答案 1 :(得分:0)
你需要这样的东西
控制器:
public ViewResult Registrants() {
IList<CompetitionNames> names = new List<CompetitionNames>();
foreach (Competition comp in repository.Competitions) {
names.Add(new CompetitionNames {
EventName = comp.Name,
RegistrantNames = comp.Registrations.Select(e => e.Name).Distinct()
});
}
return View(names);
查看:
@foreach (var comp in Model) {
<tr>
<td>@comp.EventName</td>
<td>@string.Join(", ", comp.RegistrantNames)</td>
<td>@comp.RegistrantNames.Count()</td>
</tr>
}
我在Context BOOK的685页的Applied ASP.NET 4中看到了这一点