asp.net-mvc MVC 从数据库中检索数据并在视图中显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19075469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:11:06  来源:igfitidea点击:

MVC Retrieving Data From DB and Displaying it in View

asp.net-mvcasp.net-mvc-4

提问by kayze

I am not 100% how to tackle this.

我不是 100% 如何解决这个问题。

I have a basic controller who retrieves data from a table called transactions

我有一个基本控制器,它从一个名为事务的表中检索数据

CONTROLLER private NWBA_DBEntities db = new NWBA_DBEntities();

控制器私有 NWBA_DBEntities db = new NWBA_DBEntities();

    public ActionResult Index()
    {
        var statements = db.Transactions.ToList();
        return View(statements);

    }

I have basic model:

我有基本模型:

public class StatementModel
{

    [StringLength(50)]
    [Display(Name="Transaction Type: ")]
    public string TransactionType { get; set; }

    [StringLength(50)]
    [Display(Name = "AccountNumber")]
    public string AccountNumber { get; set; }



}

My problem is at the View, i cant seem to display the values in a table:

我的问题是在视图中,我似乎无法在表中显示值:

@model Login.Models.StatementModel

@{
    ViewBag.Title = "Statements";
}


<h2>Index</h2>

<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.TransactionType)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AccountNumber)
        </th>

        <th></th>
    </tr>

    @foreach (var item in Model) 
    {
    <tr>

    </tr>
}
</table>

Can some one please help

有人可以帮忙吗

回答by Manikandan Sigamani

public ActionResult Index()
    {
        var statementModel = from s in db.Transactions
                            select new StatementModel
                            {
                                TransactionType  = s.TransactionType,
                                AccountNumber = s.AccountNumber
                            }
        return View(statementModel);

    }