简体   繁体   English

如何从C#MVC中的记录获取数据

[英]How do i get data from records in C# MVC

I have this code to get the list of record from database, in the list of iDConfig there are some columns i wants to get and put it in model to display as table in the view how do i loop through the list and get the wanted columns? 我有这段代码从数据库中获取记录列表,在iDConfig列表中,我想获取一些列并将其放入模型中以在视图中显示为表格如何循环浏览列表并获取所需的列?

public ActionResult iDeal_Table_Display(Guid? SA=null)
{
    var iDConfig = blergo.Get_iDealConfigs(SA, out retStatus, out errorMsg);
    ViewBag.iDconfigs = iDConfig;
    return PartialView();
}

this are the column in each row 这是每一行中的一列 在此处输入图片说明 the following image is the database columns that i want in iDConfig 下图是我要在iDConfig中使用的数据库列

using model below 使用下面的模型

public class iDealModel
{
    [Required]
    [DisplayName("Product")]
    public Guid ProductId { get; set; }

    [Required]
    [DisplayName("Request Type")]
    public Guid RequestTypeId { get; set; }

    [Required]
    [DisplayName("Sales Agreement Prefix")]
    public Guid SaPrefix { get; set; }

    [DisplayName("Calendar Code")]
    public System.Nullable<System.Guid> CalendarCode { get; set; }

    [DisplayName("Cash & Carry")]
    public bool CashnCarry { get; set; }

    [DisplayName("Free MM")]
    public bool FreeMM { get; set; }

    [DisplayName("On Contract")]
    public bool OnContract { get; set; }
}

You can do: 你可以做:

var product = iDConfig[0].Product

Or something like: 或类似的东西:

foreach(var ic in iDConfig)
{
    //get data like
    var product = ic.Product;

In your view you can do something like: 在您看来,您可以执行以下操作:

<table>
@foreach(var ic in iDConfig)
{
   <tr><td>@ic.Product</td></tr>
}
</table>

Use LINQ to map your DB entity to your model, like this: 使用LINQ将数据库实体映射到模型,如下所示:

var model = iDConfig.Select(ic => new iDealModel {
        SaPrefix = ic.PrefixSA,
        CalendarCode = ic.CodeCalendar,
        CashnCarry = ic.isCashnCarry,
        FreeMM = ic.isFreeMM,
        OnContract = ic.isOnContract,
        ProductId = ic.Product,
        RequestTypeId = ic.RequestType
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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