2017-12-19 7 views
0

関連するアカウントにリダイレクトするための '管理'リンクを取得しようとしています。 私は、ログインしたユーザーの詳細を表示し、両方のアカウントをデータベースに保持していることを確認するためにうまく動作するBankAccsControllerを持っていますが、私がしたいのは、アカウントの右側の対応するボタンをクリックするだけですその特定のアカウントの詳細に私を連れて行きます。私は、現在の2つの「ViewCurrentとViewSavings」を置き換えてパラメータを使用する新しい方法を作成する必要があることを知っていますが、それを行う方法がわからず、正しい単語をGoogleに入力することができません。アカウントを管理するmvc app c#

ePic of my accounts page

コントローラ

//View all accounts of logged in user 
    [Authorize] 
    public ActionResult Index() 
    { 
     var userId = User.Identity.GetUserId(); 
     var bankAccs = db.BankAccs.Where(a => a.AccUserId == userId).ToList(); 
     return View(bankAccs.ToList()); 
    } 

    ////View current account of logged in user 
    public ActionResult ViewCurrent() 
    { 

     var userId = User.Identity.GetUserId(); 
     ViewModels.CurrentAccVm bankVm = new ViewModels.CurrentAccVm(); 
     bankVm.BankAccList = db.BankAccs.Where(a => a.AccUserId == userId && a.AccTypeId == 1).ToList(); 
     bankVm.UserName = User.Identity.GetUserName(); 
     return View(bankVm); 
    } 

    ////View Savings account of logged in user 
    public ActionResult ViewSavings() 
    { 
     var userId = User.Identity.GetUserId(); 
     ViewModels.CurrentAccVm bankVm = new ViewModels.CurrentAccVm(); 
     bankVm.BankAccList = db.BankAccs.Where(a => a.AccUserId == userId && a.AccTypeId == 2).ToList(); 
     bankVm.UserName = User.Identity.GetUserName(); 
     return View(bankVm); 
    } 

ビュー

 @model IEnumerable<Atm11.Models.BankAcc> 

    @{ 
     ViewBag.Title = "Index"; 
    } 

    <h2>Index</h2> 

    <p> 
     @Html.ActionLink("Create New", "Create") 
    </p> 
    <table class="table"> 
     <tr> 
      <th> 
       @Html.DisplayNameFor(model => model.Balance) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.AccType.AccountType) 
      </th> 
      <th></th> 
     </tr> 

    @foreach (var item in Model) { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.Balance) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.AccType.AccountType) 
      </td> 
      <td> 
       @Html.ActionLink("Manage", "ViewAccounts", new { id = item.Id }) 

      </td> 
     </tr> 
    } 

    </table> 
+0

あなたが求めていることは私には分かりません。あなたは 'ViewAccounts'と呼ばれるアクションを指示するリンクを持っています。あなたはその行動を作りましたか?あなたがそれを作成するのを止めているのは何ですか? – David

+0

私は1つを作りませんでした。それは私が確信している部分です。メソッドを記述する方法と、パラメータを正しく渡すための正しい構文を知らない。 Cheers – robbiedarza

+0

正直なところ、ASP.NET MVCに関する入門チュートリアルがあります。アクションメソッドの作成は、一般的にそこでカバーされています。ヒントとして:あなたの新しいメソッドは "ViewAccounts"と呼ばれ、 "id"というパラメータを持ち、 "ActionResult"を返します。 – David

答えて

2

あなたはとても接近しています!あなたのコントローラに以下のメソッドを追加します。ビューが正しく構築されているため、そのファイルを変更する必要はありません。

コントローラ

public ActionResult ViewAccounts(int id) 
{ 

    var myAccount = DB.Accounts.GetByID(id); 
    return View(myAccount); 
} 

次は、ViewAccountsビューを作成する必要があります。

+0

ありがとうございました。私はそれを行ってあげるよ。 – robbiedarza

+0

@robbiedarzaこの回答には受け入れてください。これはあなたが尋ねた質問に対する答えです。この質問の今後の展開については、新しい質問を作成してください。 –

+0

ありがとうございました。 – robbiedarza

関連する問題