I have two tables displayed on one page in dotnet core.
- First table only displays data
- Second table has one input field
The problem:
Table has a variable count of rows in it (plus users are allowed to use filters to narrow down the amount of data displayed). Users are allowed to add one note against each row in table two, note is stored in the same table as the table 2 is displayed from.
I need to loop through the post array and only update the rows that have been edited, and I can't get my head around that. I am still relatively new to dotnet core.
Here is my code with more explanation of what I tried already:
View:
@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">
<input type="submit" value="Save" class="btn btn-primary" />
<div class="row">
<div class="col-md-12">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<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>
@foreach (var item in Model.TBMapBalances)
{
<tr>
<td><input asp-for="TbMapId" value="@item.TbMapId" disabled/></td>
<td><input asp-for="UniqueAdp" value="@item.UniqueAdp" /></td>
<td>@item.AccountsCode</td>
<td>@item.Line</td>
<td>@item.MapResult</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
</form>
</div>
</div>
Bits from the controller:
This is just to return the view:
[Authorize]
public async Task<IActionResult> TbMapView()
{
var model = new TbMapViewModel
{
TBMapBalances = await _context.TBMapBalances.Where(x => x.Dealership == "Derby").ToListAsync(),
TBMapUniqueADP = await _context.TBMapUniqueADP.Where(x => x.DealershipU == "Derby").ToListAsync()
};
return View(model);
}
Then on POST i tried to write this, however I could not, for the life of me, find any info on how to access and loop through the post array.
[Authorize]
[HttpPost]
public async Task<IActionResult> TbMapViewEdit(int Id, TBMapBalances tbMapBalances)
{
if (Id != tbMapBalances.TbMapId)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(tbMapBalances);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TbMapBalancesExists(tbMapBalances.TbMapId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction("TbMapView");
}
return RedirectToAction("TbMapView");
}
I know that the above code is for only when you pass specific ID and you have one row to edit. I was trying to use it as a starting point, for example by using List<nameofthefield>
which was giving me lots of errors.
The biggest problem really is that I have found nothing for dotnet core on the subject which means that this is incredibly easy or I am incredibly dense.
Would appreciate help with this!
EDIT #1
Just found some articles on EditorTemplates and will work through those - is that the correct approach (Some articles are for asp.net and not the .net core)?
https://www.codeproject.com/Questions/768493/How-To-Add-or-Update-Multiple-Rows-And-Save-With-O
http://www.dotnetawesome.com/2013/09/how-to-update-multiple-row-at-once.html