简体   繁体   中英

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?

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

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:

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
    });

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