Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Save multiple rows simultaneously from the same form

$
0
0

Hi Guys,

I really hope you will be able to help me with this, I am still learning and can't get my head around this problem.

I have a table that has one empty column into which user can enter a comment. Since there is a lot of data for them to comment on, I can't really expect them to open an 'Edit' page and fill in comment one by one. Instead I need them to be able to input a bunch of comments (say 20 at a time against 20 rows) and save them all at one click of submit button.

Some aspects of .net core are still alien to me, I am still learning. Here is what I tried:

View (please note that the first table is only there to display info, its the second table that has one input field per row)

@using ASPNET_Core_1_0.Models.TbMapViewModels;
@model TbMapViewModel
@{
}<div class="wrapper wrapper-content animated fadeInRight"><div class="row"><div class="col-lg-6"><div class="row"><div class="col-md-12"><table class="table table-condensed table-bordered table-hover"><thead><tr><td><b>Expense Description</b></td><td><b>Dealership</b></td><td><b>Unique ADP Code</b></td></tr></thead><tbody>
                            @foreach (var item in Model.TBMapUniqueADP)
                            {<tr><td>@item.ExpenseDescriptionU</td><td>@item.DealershipU</td><td>@item.UniqueAdpU</td></tr>
                            }</tbody></table></div></div></div><form asp-action="TbMapViewEdit"><div class="col-lg-6"><div class="row"><input type="submit" value="Save" class="btn btn-primary" /><div class="col-md-12"><table class="table table-condensed table-bordered table-hover"><thead><tr><td><b>TEMP ID</b></td><td><b>Map To</b></td><td><b>Accounts Code</b></td><td><b>Line</b></td><td><b>Map Result</b></td></tr></thead><tbody>
                                @for (int i = 0; i < Model.TBMapBalances.Count; i++)
                                {<tr><td>
                                            @Html.DisplayFor(Model => Model.TBMapBalances[i].TbMapId)
                                            @Html.HiddenFor(Model => Model.TBMapBalances[i].TbMapId)</td><td>@Html.EditorFor(Model => Model.TBMapBalances[i].UniqueAdp, new { @class = "control-label_DI" })</td><td>@Html.DisplayFor(Model => Model.TBMapBalances[i].AccountsCode)</td><td>@Html.DisplayFor(Model => Model.TBMapBalances[i].Line)</td><td>@Html.DisplayFor(Model => Model.TBMapBalances[i].MapResult)</td></tr>
                                }</tbody></table></div></div></div></form></div></div>

Model

I've learned today that I need to use List to be able to iterate through the lines in table by the use of @for loop (as shown above). before I was trying to use IEnumerable. So I added a definition to the model for public List<TBMapBalances> TBMapBalances { get; set; }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ASPNET_Core_1_0.Models.TbMapViewModels
{
    public class TbMapViewModel
    {
       // public IEnumerable<ASPNET_Core_1_0.Models.TBMapBalances> TBMapBalances { get; set; }
        public IEnumerable<ASPNET_Core_1_0.Models.TBMapUniqueADP> TBMapUniqueADP { get; set; }
        public List<TBMapBalances> TBMapBalances { get; set; }

        public ASPNET_Core_1_0.Models.TBMapBalances TbMapId { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances UniqueAdp { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances DlrCode { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances Dealership { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances UnqDept { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances AccountsCode { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances ExpenseDescription { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances BFwd { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances CurrentMth { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances CFwd { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances Future { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances ClientDepartment { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances AccountsTab { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances Area { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances ACode { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances AreaSub { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances AsCode { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances Line { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances ImpCode { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances DcCode { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances F20 { get; set; }
        public ASPNET_Core_1_0.Models.TBMapBalances MapResult { get; set; }

        public ASPNET_Core_1_0.Models.TBMapUniqueADP TbMapUniqueId { get; set; }
        public ASPNET_Core_1_0.Models.TBMapUniqueADP UniqueAdpU { get; set; }
        public ASPNET_Core_1_0.Models.TBMapUniqueADP DealershipU { get; set; }
        public ASPNET_Core_1_0.Models.TBMapUniqueADP ExpenseDescriptionU { get; set; }

    }
}

Controller:

Now this is where I need the help with, my code doesn't throw any errors at all when at the current state - when I press Submit nothing happens and no data gets saved to the database.

however, when you uncomment line  _context.Update(tbMapViewModel.TBMapBalances);  I get an error that List<TBMapBalances> is not part of any Model and is not found.

Also, below code is something I wrote trying to follow this SO post: update-multiple-records-at-once-in-asp-net-mvc - Initially I was trying to make it Async but I was getting even more errors and couldn't continue. I thought I am going to follow it as closely as possible in hope that it will get me a working starting point.

    public class TbMapController : Controller
    {
        private readonly FPSDemoContext _context;

        public TbMapController(FPSDemoContext context)
        {
            _context = context;
        }

        [Authorize]
        public async Task<IActionResult> TbMapView()
        {
            var model = new TbMapViewModel
            {
                TBMapBalances = await _context.TBMapBalances.Where(x => x.AccountsCode == "bstvx-bca-stk-ptstk-v").ToListAsync(),
                TBMapUniqueADP = await _context.TBMapUniqueADP.Where(x => x.DealershipU == "Derby").ToListAsync(),
               // TBMapBalancesList = await _context.TBMapBalances.Where(x => x.AccountsCode == "bstvx-bca-stk-ptstk-v").ToListAsync()
            };

            return View(model);
        }

        [Authorize]
        [HttpPost]
        public IActionResult TbMapViewEdit(TbMapViewModel tbMapViewModel)
        {

            if (ModelState.IsValid)
            {
                foreach (var TbListId in tbMapViewModel.TBMapBalances)
                {
                    var getCode = _context.TBMapBalances.Where(p => p.TbMapId == TbListId.TbMapId).FirstOrDefault();

                    if (getCode != null)
                    {
                        getCode.TbMapId = TbListId.TbMapId;
                    }

                }
               // _context.Update(tbMapViewModel.TBMapBalances);
                _context.SaveChanges();

            }

            return RedirectToAction("TbMapView");
        }



        [Authorize]
        public IActionResult DragDemo()
        {
            return View();
        }

        private bool TbMapBalancesExists(int? Id)
        {
            return _context.TBMapBalances.Any(e => e.TbMapId == Id);
        }

    }

I am so desperate now to learn how to do this, how to save multiple rows at the sime time from the form.

Thanks for any help!!


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>