I have a form with multiple data, that i want to save to the Database at Once. I am new to C# but i have been able to do this with PHP in the past. See the form in the table below:
<tbody>
@{int c = 0;
foreach (var item in Model)
{
c++;
<tr>
<td>@c</td>
<td>@item.FltNo<input type="hidden" name="fleetId[]" value="@item.FltNo" /></td>
<td>@item.FleetModelName<input type="hidden" name="FleetModelId[]" value="@item.FleetModelName" /></td>
<td>@item.EngineName<input type="hidden" name="EngineName[]" value="@item.EngineName" /></td>
<td>@item.LocationName<input type="hidden" name="LocationName[]" value="@item.LocationName" /></td>
<td>@item.ServiceType<input type="hidden" name="ServiceType[]" value="@item.ServiceType" /></td>
<td>@item.ServiceSequence<input type="hidden" name="ServiceSeq[]" value="@item.ServiceSequence" /></td>
<td>@item.EstimatehrPerDay<input type="hidden" name="hrperDay[]" value="@item.EstimatehrPerDay" /></td>
<td>@item.PartNo<input type="hidden" name="PartNo[]" value="@item.PartNo" /></td>
<td>@item.PartName<input type="hidden" name="PartName[]" value="@item.PartName" /></td>
<td>@item.CostperUnit.ToString("C")<input type="hidden" name="Cost[]" value="@item.CostperUnit" /></td>
<td>@item.QtyPerService<input type="hidden" name="Qty[]" value="@item.QtyPerService" /></td>
<td>@item.TotalCost.ToString("C")<input type="hidden" name="Total[]" value="@item.TotalCost" /></td>
</tr>
}
}
</tbody>
Now in the Controller, i want to be able to get all the data and save it into the Database
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Mrptran mrptran)
{
ArrayList myArrayList = new ArrayList();
string[] fltno = Request.Form["FleetId[]"];
string[] Fltmodel = Request.Form["FleetModelId[]"];
string[] Engine = Request.Form["EngineName[]"];
string[] Location = Request.Form["LocationName[]"];
string[] Servtype = Request.Form["ServiceType[]"];
string[] ServiceSeq = Request.Form["ServiceSeq[]"];
string[] hrperDay = Request.Form["hrperDay[]"];
string[] PNo = Request.Form["PartNo[]"];
string[] PName = Request.Form["PartName[]"];
string[] Cost = Request.Form["Cost[]"];
string[] Qty = Request.Form["Qty[]"];
string[] Total = Request.Form["Total[]"];
myArrayList.Add(fltno);
myArrayList.Add(Fltmodel);
myArrayList.Add(Engine);
myArrayList.Add(Location);
myArrayList.Add(Servtype);
myArrayList.Add(ServiceSeq);
myArrayList.Add(hrperDay);
myArrayList.Add(PNo);
myArrayList.Add(PName);
myArrayList.Add(Cost);
myArrayList.Add(Qty);
myArrayList.Add(Total);
var mrp = new Mrptran
{
//Map all the inputs to the Fields in the DB - This is where the issue of "How to"
};
_context.Mrptrans.Add(mrp);
Question: Is the way of getting the Data from the form alright? and how do i map the inputs to the fields in the Database, so I can Save it to the Database?If there is a better way, kindly suggest.thanks