if (Session["UserId"] == Model.userAccount.UserId)
以下是我收到错误的代码。
代码:
@model IEnumerable<OnlineTaxiReservationSystem.Models.BookingViewModel>
@{
ViewBag.Title = "Index";
}
<h2 id="h1">Bookings Detail</h2>
<p>
@Html.ActionLink("Add New Booking", "Create")
</p>
@if (Session["User"] == null)
{
Response.Redirect(Url.Action("Login", "Account"));
}
else if (Session["LoginRole"].ToString() == "Admin")
{
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.taxi.TaxiName)
</th>
<th>
@Html.DisplayNameFor(model => model.userAccount.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.booking.BookingStartDateandTime)
</th>
<th>
@Html.DisplayNameFor(model => model.booking.BookingEndDateandTime)
</th>
<th>
@Html.DisplayNameFor(model => model.booking.UserContactNo)
</th>
<th>
@Html.DisplayNameFor(model => model.booking.UserStartingLoaction)
</th>
<th>
@Html.DisplayNameFor(model => model.booking.UserDistination)
</th>
<th>
@Html.DisplayNameFor(model => model.bookingStatus.Status)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.taxi.TaxiName)
</td>
<td>
@Html.DisplayFor(modelItem => item.userAccount.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.booking.BookingStartDateandTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.booking.BookingEndDateandTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.booking.UserContactNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.booking.UserStartingLoaction)
</td>
<td>
@Html.DisplayFor(modelItem => item.booking.UserDistination)
</td>
<td>
@Html.DisplayFor(modelItem => item.bookingStatus.Status)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.booking.BookingId }) |
@Html.ActionLink("Details", "Details", new { id = item.booking.BookingId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.booking.BookingId })
</td>
</tr>
}
</table>
}
else if (Session["UserId"] == Model.userAccount.UserId)
{
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.taxi.TaxiName)
</th>
<th>
@Html.DisplayNameFor(model => model.userAccount.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.booking.BookingStartDateandTime)
</th>
<th>
@Html.DisplayNameFor(model => model.booking.BookingEndDateandTime)
</th>
<th>
@Html.DisplayNameFor(model => model.booking.UserContactNo)
</th>
<th>
@Html.DisplayNameFor(model => model.booking.UserStartingLoaction)
</th>
<th>
@Html.DisplayNameFor(model => model.booking.UserDistination)
</th>
<th>
@Html.DisplayNameFor(model => model.bookingStatus.Status)
</th>
<th></th>
</tr>
@foreach (var item in @Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.taxi.TaxiName)
</td>
<td>
@Html.DisplayFor(modelItem => item.userAccount.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.booking.BookingStartDateandTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.booking.BookingEndDateandTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.booking.UserContactNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.booking.UserStartingLoaction)
</td>
<td>
@Html.DisplayFor(modelItem => item.booking.UserDistination)
</td>
<td>
@Html.DisplayFor(modelItem => item.bookingStatus.Status)
</td>
</tr>
}
</table>
}
答案 0 :(得分:1)
模型是BookingViewModel
的枚举,因此该语句将失败:
else if (Session["UserId"] == Model.userAccount.UserId)
因为Model
是一组商品,而不仅仅是一件商品。
您需要做的是查找用户帐户并显示它:
//This is the else statement that was giving you trouble. Change it to:
else
{
string sessionId = Session["UserId"] as string;
var account = Model.FirstOrDefault(a => a.userAccount.UserId == sessionId);
if(account != null)
{
//Use your display code here
}
else
{
//Figure out what to do if for some odd reason, you can't find it.
}
}
快速代码示例假定userId
为string
。根据需要使用铸造。
我建议创建一个部分视图来显示字段而不是代码重复。
答案 1 :(得分:0)
您可以通过User
的{{1}}属性执行此操作。不要在会话中保存用户ID
假设您有经过身份验证的用户,并且他有角色&amp;身份。
ViewPage