How to print a one to many linq query to a view
I currently have this as my controller:
public ActionResult Index()
{
MembershipUser currentUser = Membership.GetUser();
Guid UserId = (Guid)currentUser.ProviderUserKey;
using (var db = new MatchGamingEntities())
{
var MyAccount = from m in db.Accounts
join n in db.BankTransactions on m.AccountId equals n.AccountId
where m.UserId == UserId
select new{Accounts = m, BankTransaction = n};
return View(MyAccount.SingleOrDefault());
}
}
Here is my View:
@model MatchGaming.Models.B开发者_开发知识库ankStatement
@{
ViewBag.Title = "Index";
}
<h2>Bank Statement</h2>
<a href="/Cashier/Withdrawal">Withdrawal</a> | <a href="/Cashier/Deposit">Deposit</a><br /><br />
<fieldset>
<legend>BankStatement</legend>
<p>
Balance: @Model.Balance
</p>
</fieldset>
Here is the BankStatement Model
public class BankStatement
{
public decimal Balance;
}
Currently my table "BankTransactions" could have multiple records in there with the same Account Id, so when I run this query I get a more than one bank transaction related to a given accountid. I want to be able to print this into a view, but I am not sure how my var MyAccount
is returning given that there are multiple BankTransactions.
How would I transfer this model into the view and be able to iterate through all the bank transactions associated with an AccountId?
The model in your control and in your view dose not match.
you need assign value to your model BankStatement
first, than return it to your view.
some thing like
BankStatement statement = new BankStatement();
//boxing value to statement
return View(statement);
精彩评论