I have this typical UI to edit Packages
and it’s childs Shipments
Example URL for editing pckage is: /Packages/Edit/5
Now after clicking AddShipment my adress changes to eg: /Packages/AddShipmentForPackage
, what makes app unable to refresh.
I’d like to add, remove end edit packages without changes in URL.
This is part of my Edit.cshtml
:
<table class="table">
<thead>
<tr class="row">
<th class="col-4">@Html.DisplayNameFor(model => model.Package.Shipments.First().ShipmentName)</th>
<th class="col-3">@Html.DisplayNameFor(model => model.Package.Shipments.First().DeliveryAddress)</th>
<th class="col-3">@Html.DisplayNameFor(model => model.Package.Shipments.First().CreatedDate)</th>
<th class="col-1">@Html.DisplayNameFor(model => model.Package.Shipments.First().Weight)</th>
<th class="col-1"></th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Package.Shipments.Count; i++)
{
<tr class="row">
@Html.HiddenFor(m => m.Package.Shipments[i].ShipmentId)
@Html.HiddenFor(m => m.Package.Shipments[i].PackageId)
<td class="col-4">
<input asp-for="@Model.Package.Shipments[i].ShipmentName" class="form-control" />
</td>
<td class="col-3">
<input asp-for="@Model.Package.Shipments[i].DeliveryAddress" class="form-control" />
</td>
<td class="col-3">
<input asp-for="@Model.Package.Shipments[i].CreatedDate" class="form-control" />
</td>
<td class="col-1">
<input asp-for="@Model.Package.Shipments[i].Weight" class="form-control" />
</td>
<td class="col-1">
<button type="submit" asp-action="RemoveShipmentForPackage" asp-route-id="@Model.Package.Shipments[i].ShipmentId" class="btn btn-danger"><i class="bi bi-trash"></i></button>
</td>
</tr>
}
</tbody>
</table>
<div class="package-form--add-shipment">
<button type="submit" asp-action="AddShipmentForPackage" class="btn btn-success">Add Shipment</button>
</div>
and this are my methods from controller:
[HttpPost]
public async Task<IActionResult> Edit(int id, PackageViewModel packageViewModel)
{
var package = packageViewModel.Package;
try
{
await _repo.EditPackageAsync(package, packageViewModel.GetShipmentsToRemove());
}
catch (DbUpdateConcurrencyException e)
{
ViewBag.ErrorMessage = e.Message;
return View("Edit", package);
}
return RedirectToAction(nameof(Index));
}
[HttpPost]
public IActionResult AddShipmentForPackage(PackageViewModel packageViewModel)
{
packageViewModel.Package.Shipments.Add(new Shipment
{
ShipmentId = 0,
ShipmentName = "",
DeliveryAddress = "",
CreatedDate = DateTime.Now,
Weight = 0
});
var view = View("Edit", packageViewModel);
return View("Edit", packageViewModel);
}
[HttpPost]
public IActionResult RemoveShipmentForPackage(int id, PackageViewModel packageViewModel)
{
packageViewModel.Package.Shipments.RemoveAll(s => s.ShipmentId == id);
packageViewModel.AddShipmentToRemove(id);
return View("Edit", packageViewModel);
}