这是我的模特课。是的,我的模型类有集合属性
块引用
[Table("Booking")]
public partial class Booking
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Booking()
{
Cancels = new HashSet<Cancel>();
}
[Key]
[Column("BookingId")]
[Display(Name = "Booking Id")]
[Required(ErrorMessage = "Booking Id is required...")]
public int BookingId { get; set; }
[Column("BookingStartDateandTime")]
[Display(Name = "Booking Start Date and Time"), Required(ErrorMessage = "Booking Start Date and Time is reqired...")]
public DateTime BookingStartDateandTime { get; set; }
[Column("BookingEndDateandTime")]
[Display(Name = "Booking End Date and Time"), Required(ErrorMessage = "Booking End Date and Time is reqired...")]
public DateTime BookingEndDateandTime { get; set; }
[Column("UserContactNo")]
[Required(ErrorMessage = "User Contact No is required..."), Display(Name = "User Contact No")]
[StringLength(50)]
public string UserContactNo { get; set; }
[Column("UserStartingLoaction")]
[Required(ErrorMessage = "User Starting Loaction is required..."), Display(Name = "User Starting Loaction")]
public string UserStartingLoaction { get; set; }
[Column("UserDistination")]
[Required(ErrorMessage = "User Distination is required..."), Display(Name = "User Distination")]
public string UserDistination { get; set; }
[Column("StatusId")]
[Display(Name = "StatusId")]
public int? StatusId { get; set; }
public virtual BookingStatus BookingStatus { get; set; }
[Column("TaxiId")]
[Display(Name = "Taxi Name")]
public int? TaxiId { get; set; }
public virtual Taxi Taxi { get; set; }
[Column("UserId")]
[Display(Name = "User Id")]
public int? UserId { get; set; }
public virtual UserAccount UserAccount { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Cancel> Cancels { get; set; }
}
}
如何使用foreach循环显示登录用户记录
if (Session["LoginRole"].ToString() == "User"
&&Session["UserId"].ToString() ==
Model.FirstOrDefault().UserId.ToString())
{
<table class="table">
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.BookingStatus.Status)
</td>
<td>
@Html.DisplayFor(modelItem => item.Taxi.TaxiName)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserAccount.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.BookingStartDateandTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.BookingEndDateandTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserContactNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserStartingLoaction)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserDistination)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.BookingId})|
@Html.ActionLink("Details", "Details", new { id = item.BookingId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.BookingId })
</td>
</tr>
}
</table>
}
这是控制器
public ActionResult Index()
{
var bookings = db.Bookings.Include(u => u.UserAccount).Include(t =>
t.Taxi).Include(s => s.BookingStatus);
return View(db.Bookings.ToList());
答案 0 :(得分:0)
Index()动作通常用于显示集合。您已获得指向Details()操作的链接,该操作通常显示单个记录。所以你应该有一个细节行动:
public ActionResult Details(int bookingId)
{
var booking = db.Bookings
.Include(u => u.UserAccount)
.Include(t => t.Taxi)
.Include(s => s.BookingStatus)
.Single(b => b.BookingId = bookingId);
return View(booking);
}
然后,您将创建一个与此类似的详细信息视图:
@model Booking
@{
ViewBag.Title = "Booking Details";
}
<h2>Booking Details</h2>
<div>
<dt>Status</dt>
<dd>
@Html.DisplayFor(model=> model.BookingStatus.Status)
</dd>
<dt>Taxi Name</dt>
<dd>
@Html.DisplayFor(model=> model.Taxi.TaxiName)
</dd>
[repeat for other fields]
</dl>
</div>
}