A MVC create view has two sections for Invoice and InvoiceItem tables.
Invoice details section is with default scaffolding code for create.
Invoice item section has a button "Add Item" and a partial view. When the user clicks "Add Item", a jQuery UI pop will be shown to create Invoice items and it calls /Invoice/GetInvoiceItems action. This method appends the new invoice item with existing items(if any) and returns the partial view that is loaded in a Div(viewAPInvoiceIndex). This partial view has default scaffolding code for list.
The created invoice items are not stored to DB till the user creates a complete invoice. Also the user will be able to edit/delete invoice items.
Create.chtml:
<div class="panel-body">
<div id="viewAPInvoiceIndex">
@{ Html.RenderPartial("_APInvoiceIndex", (IEnumerable<TransportationModule.Models.APInvoiceItem>)@ViewBag.CurrInvItems); }
</div>
</div>
Add invoice items code(for the button in the pop up):
$("#btnAddItem").click(function () {
var currId = $('[id$=tableInvItems] tr').length;
if (currId < 1)
currId = 1;
var _item = { Id: currId, InvoiceNo: $('#txtInvNo').val(), OrderQty: $('#txtOrderQty').val(), UnitPrice: $('#txtUnitPrice').val()
};
$("[id$=viewAPInvoiceIndex]").load("/Invoice/GetInvoiceItems", { InvItem: _item });
$('#mdGrnList').dialog("close");
});
Action methods:
public ActionResult Create()
{
IList<APInvoiceItem> existingItems = new List<APInvoiceItem>();
if (Session != null && Session["ExistingItems"] != null)
existingItems = (IList<APInvoiceItem>)Session["ExistingItems"];
ViewBag.CurrInvItems = existingItems;
return View();
}
// UPDATES THE MODEL WITH NEW ITEM AND RETURNS THE PARTIAL VIEW
public ActionResult GetInvoiceItems(APInvoiceItem InvItem)
{
IList<APInvoiceItem> existingItems = new List<APInvoiceItem>();
if (Session != null && Session["ExistingItems"] != null)
existingItems = (IList<APInvoiceItem>)Session["ExistingItems"];
existingItems.Add(InvItem);
Session["ExistingItems"] = existingItems;
return PartialView("_APInvoiceIndex", existingItems);
}
// REMOVES AN INVOICE ITEM
public ActionResult DeleteInvItem(int id)
{
IList<APInvoiceItem> existingItems = new List<APInvoiceItem>();
if (Session != null && Session["ExistingItems"] != null)
existingItems = (IList<APInvoiceItem>)Session["ExistingItems"];
var itemToRemove = existingItems.SingleOrDefault(r => r.Id == id);
if (itemToRemove != null)
existingItems.Remove(itemToRemove);
Session["ExistingItems"] = existingItems;
return PartialView("_APInvoiceIndex", existingItems);
}
I am able to create invoice items and partial view is loaded correctly. But if I add a new item and press Edit/Delete nothing happens. Seems the corresponding jQuery methods are not binded till the page is refreshed. Once the page is refreshed, I am able to delete an item.
_APInvoiceIndex.chtml
<td>
<button class="btn btn-danger btn-sm" id="btnDeleteItem" data-id="@item.Id" type="button"></button>
</td>
$('[id$=btnDeleteItem]').click(function () {
var invId = $(this).attr("data-id");
$.ajax({
url: '/Invoice/DeleteInvItem',
data: { id: invId },
success: function (data) {
$("[id$=viewAPInvoiceIndex]").html(data);
},
error: function (request, textStatus, errorThrown) {
alert(textStatus + " " + errorThrown);
}
});
});
Also please advice whether this is the correct way to implement the above said scenario(creation of Invoice and InvoiceItems).
Thanks for your time.