如何将会话值与当前用户ID进行比较

时间:2017-11-06 19:30:45

标签: c# asp.net-mvc razor

  1. 如何将会话值与当前用户ID进行比较。
  2. 仅显示该用户登录的记录。
  3. 在这里,我加入了三个表来制作这个视图。
  4. if (Session["UserId"] == Model.userAccount.UserId)以下是我收到错误的代码。
  5. enter image description here

    代码:

    @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>
       }
    

2 个答案:

答案 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.
    }
}

快速代码示例假定userIdstring。根据需要使用铸造。

我建议创建一个部分视图来显示字段而不是代码重复。

答案 1 :(得分:0)

您可以通过User的{​​{1}}属性执行此操作。不要在会话中保存用户ID 假设您有经过身份验证的用户,并且他有角色&amp;身份。

ViewPage