简体   繁体   中英

Entity Framework Display SQL Query Results in View

So I have a view where I am trying to show all the transactions on a single account.

This is the SQL

    Select Distinct TransactionAmount,DateOfTransaction
    From BankAccountModels
    Inner Join TransactionModels on BankAccountModels.AccountID=TransactionModelID

And I'm trying to display the results in my view

This is in my controller

    // GET: /BankAccount/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        BankAccountModel bankaccountmodel = db.BankAccountModels.Find(id);
        if (bankaccountmodel == null)
        {
            return HttpNotFound();
        }
       // string query = "Select Distinct TransactionAmount,DateOfTransaction From BankAccountModels Inner Join TransactionModels on BankAccountModels.AccountID=TransactionModelID";
       // IEnumerable<BankAccountModel> data = db.Database.SqlQuery<BankAccountModel>(query);
        return View(bankaccountmodel);
    }

This is my current view

    @model Bank7round2.Models.BankAccountModel

    @{
        ViewBag.Title = "Details";
    }

    <h2>Details</h2>

    <div>
        <h4>BankAccountModel</h4>
        <hr />
        <dl class="dl-horizontal">
            <dt>
                @Html.DisplayNameFor(model => model.Balance)
            </dt>

            <dd>
                @Html.DisplayFor(model => model.Balance)
            </dd>

            <dt>
                @Html.DisplayNameFor(model => model.UserName)
            </dt>

            <dd>
                @Html.DisplayFor(model => model.UserName)
            </dd>

        </dl>
    </div>
    <p>
        @Html.ActionLink("Edit", "Edit", new { id = Model.AccountID }) |
        @Html.ActionLink("Back to List", "Index")
    </p>

How would I go about this?

If the model you pass to your view implements IEnumerable, then you can do something like this:

<table>
    <tr>
        <th>Transaction ID</th>
        <th>Amount</th>
    </tr>

@foreach(var transaction in YourTransactionModel)
{
    <tr>
        <td>@transaction.ID</td>
        <td>@transaction.Amount</td>
    </tr>
}

</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM