The field names must be indexed otherwise the model binder does not know a collection is passed. Please see the following blog.
ASP.Net Core Post a Collection
Ali Yılmaz
81
Reputation points
Hi,
I'm bringing the data into the html table. This data also has more than one row. Then I want to save this data in database. But it seems empty.
What is the solution to this?
Classes
public class DusumDto
{
public string BeyannameNO { get; set; }
public string StokKodu { get; set; }
public string ArtikelName { get; set; }
public string TicariTanim { get; set; }
public string Karisim { get; set; }
public decimal Agirlik { 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 int Adet { get; set; }
public int KoliAdet { get; set; }
public double? BrutKg { get; set; }
public double? NetKg { get; set; }
}
public class VM_Dusum : DusumDto
{
public List<DusumDto> Dusumler { get; set; }
}
View
<form asp-controller="Dusum" asp-action="DusumAdd">
<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 (Model != null)
{
@foreach (var item in Model.Dusumler)
{
<tr class="bg-dark text-white">
<td>
@item.StokKodu
</td>
<td>
@item.ArtikelName
</td>
<td>@item.BeyannameNO</td>
<td>@item.TicariTanim</td>
<td>@item.Karisim</td>
<td>@item.Agirlik</td>
<td>@item.Mensei</td>
<td>@item.GtipNo</td>
<td>@item.Marka</td>
<td>@item.Artikel</td>
<td>@item.KumasKodu</td>
<td>@item.BirimFiyat</td>
<td>@item.ToplamFiyat</td>
<td>@item.Adet</td>
<td>@item.KoliAdet</td>
</tr>
<input hidden asp-for="@item.StokKodu" type="text">
<input hidden asp-for="@item.ArtikelName" type="text">
<input hidden asp-for="@item.BeyannameNO" type="text">
<input hidden asp-for="@item.TicariTanim" type="text">
<input hidden asp-for="@item.Karisim" type="text">
<input hidden asp-for="@item.Agirlik" type="text">
<input hidden asp-for="@item.Mensei" type="text">
<input hidden asp-for="@item.GtipNo" type="text">
<input hidden asp-for="@item.Marka" type="text">
<input hidden asp-for="@item.Artikel" type="text">
<input hidden asp-for="@item.KumasKodu" type="text">
<input hidden asp-for="@item.BirimFiyat" type="text">
<input hidden asp-for="@item.ToplamFiyat" type="text">
<input hidden asp-for="@item.Adet" type="text">
<input hidden asp-for="@item.KoliAdet" type="text">
}
}
</tbody>
</table>
</div>
</div>
</div>
<div class="form-group ">
<button type="submit" style="float:right" class="text-right btn btn-primary">Düşüm Ekle</button>
</div>
</form>
Controller
public IActionResult DusumAdd(VM_Dusum vM_Dusum)
{
return View();
}
2 answers
Sort by: Most helpful
-
-
Ali Yılmaz 81 Reputation points
2022-07-06T10:59:25.913+00:00 Hi,
First of all, thanks. It happened as follows. But how do I do it if I need to do it with foreach. I was wondering about him.
for (int i = 0; i < Model.Dusumler.Count; i++) { <tr class="bg-dark text-white"> <td> <input value="@Model.Dusumler[i].StokKodu" name="[@i].StokKodu" type="text"> </td> <td> <input value="@Model.Dusumler[i].ArtikelName" name="[@i].ArtikelName" type="text"> </td> </tr>