我已经坚持了将近2天,我一直在尝试解决问题,但似乎无法获得所需的数据。
我是ASP.NET的新手。每当我单击某个事件的详细信息(链接)时,我都希望让某个事件的所有与会者参加。
我认为我的逻辑正确,但是似乎无法用代码编写。
这是我的details.cshtml
文件:
@model MVCUML.Models.Registration
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Registration</h4>
<hr />
@Html.DisplayForModel(Model.Events.EventName)
<table>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayForModel(Model.Attendees.Name) </td>
</tr>
}
</table>
</div>
<p>
@Html.ActionLink("Back to List", "Index")
</p>
这是我的注册/详细信息控制者:
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Registration registration = db.Registrations.Find(id);
if (registration == null)
{
return HttpNotFound();
}
return View(registration);
}
namespace MVCUML.Models
{
public class Registration
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public int EventId { get; set; }
[Required]
public int AttendeeId { get; set; }
public virtual Event Events { get; set; }
public virtual Attendee Attendees { get; set; }
}
}
public class Event
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[Display(Name = "Event Name")]
public string EventName { get; set; }
[Required]
public string Location { get; set; }
[Required]
[Display(Name = "Event Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime EventDate { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:C}")]
public Decimal Fee { get; set; }
public virtual ICollection<EventAssignment> EventAssignments { get; set; }
public virtual ICollection<Registration> Registrations { get; set; }
}
public class Attendee
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[Display(Name = "First Name")]
public string FName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LName { get; set; }
public string Name
{
get { return string.Format("{0} {1}", this.FName, this.LName); }
}
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[Phone]
public string Phone { get; set; }
public virtual ICollection<Registration> Registrations { get; set; }
}
public class RegistrationIndexData
{
public IEnumerable<Event> Events { get; set; }
public IEnumerable<Attendee> Attendees { get; set; }
public IEnumerable<Registration> Registrations { get; set; }
}
答案 0 :(得分:0)
用以下代码替换您的foreach循环代码,然后尝试一下:
<table>
@foreach (var evtReg in Model.Events.Registrations)
{
<tr>
<td>@evtReg.Attendees.Name </td>
</tr>
}
</table>
此外,我建议将Events
和Attendees
重命名为诸如Event
类的Attendee
和Registration
属性之类的单数名称,因为这些属性保留在object和不是对象列表
答案 1 :(得分:0)
我想我明白了!需要对代码进行一些调整
代替此
<td>@Html.DisplayForModel(evtReg.Attendees.Name) </td>
我将其更改为:
<td>@evtReg.Attendee.Name</td>
它就像一个魅力。非常感谢!