locked
Save Form Array Data at Once into Database RRS feed

  • Question

  • User-1645198287 posted

    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

    Thursday, March 12, 2020 9:23 AM

Answers

  • User-854763662 posted

    Hi kingsadave ,

    What's the code of the model in your view and the Mrptran model that you want to save the data to ?

    If the properties of the model in view are same with the properties of  Mrptran model , you could change the name attribute of input and save the data in controller as follows:

    View:

    @model IEnumerable<MVCDemo3_0.Models.ViewModels.MrptranVM>
    @{
        ViewData["Title"] = "SaveDataList";
        int i = 0;
    }
    
    <form method="post" asp-action="Create">
        <table class="table-bordered">
            <tr>
                <th>FltNo</th>
                <th>FleetModelName</th>
                <th> EngineName</th>
                <th> LocationName</th>
                <th> ServiceType</th>
                <th> ServiceSequence</th>
                <th> EstimatehrPerDay</th>
                <th> PartNo</th>
                <th> PartName</th>
                <th> CostperUnit</th>
                <th> QtyPerService</th>
                <th> TotalCost</th>
            </tr>
            @foreach (var item in Model)
            {
                <tr>
                    <td>@item.FltNo<input type="hidden" name="mrptranList[@i].FltNo" value="@item.FltNo" /></td>
                    <td>@item.FleetModelName<input type="hidden" name="mrptranList[@i].FleetModelName" value="@item.FleetModelName" /></td>
                    <td>@item.EngineName<input type="hidden" name="mrptranList[@i].EngineName" value="@item.EngineName" /></td>
                    <td>@item.LocationName<input type="hidden" name="mrptranList[@i].LocationName" value="@item.LocationName" /></td>
                    <td>@item.ServiceType<input type="hidden" name="mrptranList[@i].ServiceType" value="@item.ServiceType" /></td>
                    <td>@item.ServiceSequence<input type="hidden" name="mrptranList[@i].ServiceSequence" value="@item.ServiceSequence" /></td>
                    <td>@item.EstimatehrPerDay<input type="hidden" name="mrptranList[@i].EstimatehrPerDay" value="@item.EstimatehrPerDay" /></td>
                    <td>@item.PartNo<input type="hidden" name="mrptranList[@i].PartNo" value="@item.PartNo" /></td>
                    <td>@item.PartName<input type="hidden" name="mrptranList[@i].PartName" value="@item.PartName" /></td>
                    <td>@item.CostperUnit.ToString("C")<input type="hidden" name="mrptranList[@i].CostperUnit" value="@item.CostperUnit" /></td>
                    <td>@item.QtyPerService<input type="hidden" name="mrptranList[@i].QtyPerService" value="@item.QtyPerService" /></td>
                    <td>@item.TotalCost.ToString("C")<input type="hidden" name="mrptranList[@i].TotalCost" value="@item.TotalCost" /></td>
                </tr>
                i++;
            }
        </table>
        <input type="submit" value="AddAll"/>
    </form>
    

    Controller:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Create(List<Mrptran> mrptranList)
    {
         _context.Mrptran.AddRange(mrptranList);
         _context.SaveChanges();
         return RedirectToAction(nameof(SaveDataList));
    }

    Result:

    Best Regards,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 13, 2020 7:19 AM

All replies

  • User-474980206 posted

    the binding syntax is different for asp.net then PHP. in asp.net you include the index in the binding string:

    @{int c = 0;
    foreach (var item in Model)
    {
    c++;
    
    <tr>
    <td>@c</td>
    <td>@item.FltNo<input type="hidden" name="@($"fleetId[{c}]")" value="@item.FltNo" /></td>
    

    Thursday, March 12, 2020 2:34 PM
  • User-1645198287 posted

    Thanks, so how do I Read  and store the Input into the Database

    Thursday, March 12, 2020 3:33 PM
  • User-474980206 posted

    as you changed the binding names from the property names, you will need a different post back model that matches the binding. if you make the binding names match the property names (or use tag helpers), then the post back data will bind to the original model.

    to read and write to database depends on which database library you are using. 

    it appears you are skipping any tutorials and jumping into converting code. as C# is a strongly typed language, the framework support strongly typed model binding. also you should learn EF which is a strongly typed ORM.

     

    Thursday, March 12, 2020 4:05 PM
  • User-854763662 posted

    Hi kingsadave ,

    What's the code of the model in your view and the Mrptran model that you want to save the data to ?

    If the properties of the model in view are same with the properties of  Mrptran model , you could change the name attribute of input and save the data in controller as follows:

    View:

    @model IEnumerable<MVCDemo3_0.Models.ViewModels.MrptranVM>
    @{
        ViewData["Title"] = "SaveDataList";
        int i = 0;
    }
    
    <form method="post" asp-action="Create">
        <table class="table-bordered">
            <tr>
                <th>FltNo</th>
                <th>FleetModelName</th>
                <th> EngineName</th>
                <th> LocationName</th>
                <th> ServiceType</th>
                <th> ServiceSequence</th>
                <th> EstimatehrPerDay</th>
                <th> PartNo</th>
                <th> PartName</th>
                <th> CostperUnit</th>
                <th> QtyPerService</th>
                <th> TotalCost</th>
            </tr>
            @foreach (var item in Model)
            {
                <tr>
                    <td>@item.FltNo<input type="hidden" name="mrptranList[@i].FltNo" value="@item.FltNo" /></td>
                    <td>@item.FleetModelName<input type="hidden" name="mrptranList[@i].FleetModelName" value="@item.FleetModelName" /></td>
                    <td>@item.EngineName<input type="hidden" name="mrptranList[@i].EngineName" value="@item.EngineName" /></td>
                    <td>@item.LocationName<input type="hidden" name="mrptranList[@i].LocationName" value="@item.LocationName" /></td>
                    <td>@item.ServiceType<input type="hidden" name="mrptranList[@i].ServiceType" value="@item.ServiceType" /></td>
                    <td>@item.ServiceSequence<input type="hidden" name="mrptranList[@i].ServiceSequence" value="@item.ServiceSequence" /></td>
                    <td>@item.EstimatehrPerDay<input type="hidden" name="mrptranList[@i].EstimatehrPerDay" value="@item.EstimatehrPerDay" /></td>
                    <td>@item.PartNo<input type="hidden" name="mrptranList[@i].PartNo" value="@item.PartNo" /></td>
                    <td>@item.PartName<input type="hidden" name="mrptranList[@i].PartName" value="@item.PartName" /></td>
                    <td>@item.CostperUnit.ToString("C")<input type="hidden" name="mrptranList[@i].CostperUnit" value="@item.CostperUnit" /></td>
                    <td>@item.QtyPerService<input type="hidden" name="mrptranList[@i].QtyPerService" value="@item.QtyPerService" /></td>
                    <td>@item.TotalCost.ToString("C")<input type="hidden" name="mrptranList[@i].TotalCost" value="@item.TotalCost" /></td>
                </tr>
                i++;
            }
        </table>
        <input type="submit" value="AddAll"/>
    </form>
    

    Controller:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Create(List<Mrptran> mrptranList)
    {
         _context.Mrptran.AddRange(mrptranList);
         _context.SaveChanges();
         return RedirectToAction(nameof(SaveDataList));
    }

    Result:

    Best Regards,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 13, 2020 7:19 AM
  • User-1645198287 posted

    I have been able to get the Answer. Pls refer to  this link:

    https://stackoverflow.com/questions/60667059/save-form-array-data-at-once-into-database-in-aspnetcore-mvc/60668323#60668323

    Friday, March 13, 2020 10:01 AM
  • User-1645198287 posted

    Thank you so so much. It works.

    Friday, March 13, 2020 10:38 AM