List in Code First Class

Ali Yılmaz 81 Reputation points
2022-06-08T18:10:25.397+00:00

My goal here is to read data from excel and show the data to the following html table.

In other words, I will pull the data from the Model and put it in the List from this model, and then I will draw the data to the table. In this case, how should the class construction be?

I shared my codes. Can you make an example accordingly?

Also, how should I research to learn such structures?

Classes

     public class Dusumler : BaseEntity  
    {  
        public string BeyannameNO { get; set; }  
  
        public string StokKodu { get; set; }  
  
        public int KoliAdet { get; set; }  
  
  
    }  
	  
	  
	 public class DusumDto  
    {  
          
	public string BeyannameNO { get; set; }  
		  
        public string StokKodu { get; set; }  
		  
	public int KoliAdet { get; set; }  
  
    }  
	  
	Web Project Models  
    public class VM_Dusum   
    {  
        public List<Dusumlers> Dusumler { get; set; }  
    }  

Controller
[HttpPost]
public IActionResult Index(IFormFile file)
{
string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\Files");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);

            FileInfo fileInfo = new FileInfo(file.FileName);  
            string fileName = file.FileName;  
  
            string fileNameWithPath = Path.Combine(path, fileName);  
  
            //var stream = new FileStream(fileNameWithPath, FileMode.Create);  
  
            using (var stream = new FileStream(fileNameWithPath, FileMode.Create))  
            {  
                file.CopyToAsync(stream);  
            }  
  
            WorkBook workbook = WorkBook.Load(System.IO.File.OpenRead(fileNameWithPath));  
            WorkSheet sheet = workbook.WorkSheets.First();  
  
            string stok = sheet["A2"].StringValue;  
            string artikeladi = sheet["B2"].StringValue;  
            string bno = sheet["C2"].StringValue;  
            string ticaritanim = sheet["D2"].StringValue;  
            string karisim = sheet["E2"].StringValue;  
            decimal agirlik = sheet["F2"].DecimalValue;  
  
            string mensei = sheet["G2"].StringValue;  
            string gtip = sheet["H2"].StringValue;  
            string marka = sheet["I2"].StringValue;  
            string artikel = sheet["J2"].StringValue;  
            string kumaskodu = sheet["K2"].StringValue;  
  
            decimal birimfiyat = sheet["L2"].DecimalValue;  
            decimal toplamfiyat = sheet["M2"].DecimalValue;  
            int adet = sheet["N2"].IntValue;  
            int koliadet = sheet["O2"].IntValue;  
  
  
  
            Dusumlers dusum = new Dusumlers();  
              
              
            dusum.StokKodu = stok;  
            dusum.ArtikelName = artikeladi;  
            dusum.BeyannameNO = bno;  
            dusum.TicariTanim = ticaritanim;  
            dusum.Karisim = karisim;  
            dusum.Agirlik = agirlik;  
            dusum.Mensei = mensei;  
            dusum.GtipNo = gtip;  
            dusum.Marka = marka;  
            dusum.Artikel = artikel;  
            dusum.KumasKodu = kumaskodu;  
            dusum.BirimFiyat = birimfiyat;  
            dusum.ToplamFiyat = toplamfiyat;  
            dusum.Adet = adet;  
            dusum.KoliAdet = koliadet;  
  
            VM_Dusum dusums = new VM_Dusum();  
  
            dusums.Dusumler.Add(dusum);  
  
             
  
            return View();  
        }  

View

@{  
    ViewData["Title"] = "Index";  
    Layout = "~/Views/Shared/_Layout.cshtml";  
}  
  
  
  
<div class="content-page">  
    <div class="content">  
  
        <div class="container-fluid card-box table-responsive">  
            <div class="col-md-6">  
  
                <form asp-controller="Dusum" asp-action="Index" enctype="multipart/form-data">  
  
                    <div class="form-group">  
                        <p>Düşüm ekle</p>  
  
  
                        <input type="file" name="file" class="filestyle" id="filestyleicon">  
  
  
  
                        <button type="submit" style="margin-left:10px;" class="text-right btn btn-primary">Önizleme</button>  
  
                    </div>  
  
  
  
                </form>  
  
            </div>  
  
            <div class="col-md-12">  
  
                <div class="table-responsive">  
                    <div class="table-responsive">  
  
                        <table class="table table-bordered mb-0">  
                            <thead>  
                                <tr>  
  
                                    <th>Stok Kodu</th>  
                                    <th>Artikel Adı</th>  
                                    <th>Beyanname No</th>  
                                    <th>Ticari Tanım</th>  
                                    <th>Karışım</th>  
                                    <th>Ağırlık</th>  
  
                                    <th>Menşei</th>  
                                    <th>Gtip No</th>  
                                    <th>Marka</th>  
                                    <th>Artikel</th>  
                                    <th>Kumaş Kodu</th>  
                                    <th>Birim Fiyat</th>  
  
                                    <th>Toplam Fiyat</th>  
                                    <th>Adet</th>  
                                    <th>Koli Adet</th>  
                                </tr>  
                            </thead>  
                            <tbody>  
                           
                                @if (ViewBag.Dusum != null)  
                                {  
  
  
  
                                    @foreach (var item in ViewBag.Dusum)  
                                    {  
  
  
                                        <tr class="bg-dark text-white">  
  
                                            <td></td>  
                                            <td></td>  
                                            <td></td>  
                                            <td>Gtip</td>  
                                            <td>KalemSayisi</td>  
                                            <td>Adet</td>  
                                        </tr>  
                                    }  
                                }  
                            </tbody>  
                        </table>  
                    </div>  
                </div>  
  
            </div>  
  
        </div>  
    </div>  
</div>  
Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
698 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,208 questions
{count} votes

Accepted answer
  1. Rena Ni - MSFT 2,061 Reputation points
    2022-06-09T03:07:04.833+00:00

    Hi @Ali Yılmaz ,

    Assume that you can get the value from excel, here is a simple demo about how to show the data to the table:

    Model

    public class BaseEntity  
    {  
        public string ArtikelName { get; set; }  
        public string TicariTanim { get; set; }  
        public string Karisim { get; set; }  
        public string Mensei { get; set; }  
        public string GtipNo { get; set; }  
        public string Marka { get; set; }  
        public string Artikel { get; set; }  
        public string KumasKodu { get; set; }  
        public decimal BirimFiyat { get; set; }  
        public decimal ToplamFiyat { get; set; }  
        public decimal Agirlik { get; set; }  
        public int Adet { get; set; }  
    }  
    public class Dusumler : BaseEntity  
    {  
        public string BeyannameNO { get; set; }  
        public string StokKodu { get; set; }  
        public int KoliAdet { get; set; }  
    }  
    public class VM_Dusum  
    {  
        public List<Dusumler> Dusumler { get; set; }  
    }  
    

    View

    <table class="table table-bordered mb-0">  
        <thead>  
            <tr>      
                <th>Stok Kodu</th>  
                <th>Artikel Adı</th>  
                <th>Beyanname No</th>  
                <th>Ticari Tanım</th>  
                <th>Karışım</th>  
                <th>Ağırlık</th>      
                <th>Menşei</th>  
                <th>Gtip No</th>  
                <th>Marka</th>  
                <th>Artikel</th>  
                <th>Kumaş Kodu</th>  
                <th>Birim Fiyat</th>  
                <th>Toplam Fiyat</th>  
                <th>Adet</th>  
                <th>Koli Adet</th>  
            </tr>  
        </thead>  
        <tbody>                              
            @if (Model.Dusumler != null)  
            {      
                @foreach (var item in Model.Dusumler)  
                {  
                <tr class="bg-dark text-white">  
          
                        <td>@Html.DisplayFor(modelItem=>item.StokKodu)</td>  
                        <td>@Html.DisplayFor(modelItem=>item.Artikel)</td>  
                        <td>@Html.DisplayFor(modelItem=>item.BeyannameNO)</td>  
                        <td>@Html.DisplayFor(modelItem=>item.TicariTanim)</td>  
                        <td>@Html.DisplayFor(modelItem=>item.Karisim)</td>  
                        @*more property......*@  
                    </tr>  
                }  
            }  
        </tbody>  
    </table>  
    

    Controller

    public async Task<IActionResult> Index()  
    {  
        //more code......  
        Dusumler dusum = new Dusumler();  
        dusum.StokKodu = stok;  
        //......  
        dusum.KoliAdet = koliadet;  
    
        VM_Dusum dusums = ne w  VM_Dusum()  
       {   
             Dusumler=ne w List<Dusumler>()                     //change here.......  
      };               
        dusums.Dusumler.Add(dusum);  
    
        r eturn View(dusums);     //change here..........  
    }  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Rena

    0 comments No comments

0 additional answers

Sort by: Most helpful