2017-11-06 13 views
0
  1. セッションの値と現在のユーザーIDを比較するにはどうすればよいですか。
  2. ログインしているユーザーのレコードのみを表示する。
  3. ここでは、このビューを作成するために3つのテーブルを結合します。
  4. if (Session["UserId"] == Model.userAccount.UserId)ここで私はエラーが発生したコードです。

enter image description hereセッションの値を現在のユーザーIDと比較する方法

コード:

@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

エラーメッセージをお読みください。 – SLaks

+0

'Model.userAccount'とはどういう意味ですか? – SLaks

+0

どのようなエラーが表示されますか?ここで質問しているほとんどのユーザーには何らかのエラーがありますが、実際の例外メッセージを投稿することは、何が間違っているかを把握するのに役立ちます。 – Marco

答えて

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ある前提としています。必要に応じてキャスティングを使用します。

コードの繰り返しとは対照的に、フィールドを表示するために部分的なビューを作成することをお勧めします。

+0

で変換を行う必要があります。これは私のログインコードです var usr = db.UserAccounts.Single(u => u.Email == user.Email && u.Password == user.Password); if(usr!= null) { セッション["user"] = usr.ToString(); セッション["UserId"] = usr.UserId.ToString(); if(usr.UserRole == true) { セッション["LoginRole"] = "Admin"; } else { セッション["LoginRole"] = "ユーザー"; } – Farman

+1

@Farman:長いコードをコメントとして投稿しないでください。あなたの質問にそれを加えてください。 – JuanR

0

あなたのViewPageUserのプロパティで行うことができます。セッションにユーザーIDを保存しないでください ユーザーが認証されており、ロールに&というIDがあるとします。

public class MyPrincipal: IPrincipal 
{ 
    private MyIdentity _identity; 
    private string[] _roles; 

    public MyPrincipal(MyIdentity identity, string[] roles) 
    { 
     _identity = identity; 
     _roles = roles; 
    } 

    public bool IsInRole(string role) 
    { 
     return _roles?.Length > 0 && _roles.Contains(role); 
    } 

    IIdentity IPrincipal.Identity 
    { 
     get 
     { 
      return _identity; 
     } 
    } 

    public MyIdentity Identity 
    { 
     get { return _identity;} 
    } 

    // User ID you need 
    public int UserId 
    { 
     get 
     { 
      return Identity.UserId; 
     } 
    } 
} 
somewhere in your code 
@{ 
    int currentUserId = (User as MyPrincipal).UserId; 
} 
@if (currentUserId == Model.userId) 
{ 
    // this will be executed personally per executing user 
} 
+0

Identityを認証に使用しなかったため、IdentityなしのmyprincipalでIprincipalを使用できますか。私はログインのためにセッションだけを使用しました。 if(usr!= null) { セッション["user"] = usr。ToStringメソッド() セッション[ "ユーザーID"] = usr.UserId.ToString() IF(usr.UserRoleが== TRUE){ セッション[ "LoginRole"] = "管理者" }他 {セッション[ "LoginRole"] = "User" } – Farman

関連する問題