我对.net core还是很陌生,我不知道如何为两个实体创建具有相关数据的表单并将数据保存到dbcontext。 支持我有以下两个实体
public partial class invoice
{
public int Id;
public int invoiceNo;
public string supplierName;
public ICollection<invoiceDetail> invoiceItems {get;set;}
}
public partial class invoiceDetail
{
public int Id;
public int InvoiceId;
public string itemname;
public int quantity;
}
我如何在单个视图页面上添加发票明细,并在InvoiceDetails表中包含项目数? 预先感谢。
答案 0 :(得分:0)
如果要在添加发票明细时在InvoiceDetails表中包括项目数,则需要在发票表中添加ItemCount字段。
以下是我编写的代码中的主要代码段,您可以参考
发票表和发票明细表中的更改
public class invoice
{
public int Id { get; set; }
public int invoiceNo { get; set; }
public string supplierName { get; set; }
public int ItemCount { get; set; }
public ICollection<invoiceDetail> invoiceItems { get; set; }
}
public class invoiceDetail
{
public int Id { get; set; }
public string itemname { get; set; }
public int quantity { get; set; }
public int InvoiceId { get; set; }
public invoice Invoice { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<invoiceDetail>()
.Property<int>("InvoiceId");
modelBuilder.Entity<invoiceDetail>()
.HasOne(id => id.Invoice)
.WithMany(i => i.invoiceItems)
.HasForeignKey("InvoiceId");
}
“查看页面”,添加发票时使用javascript添加多个发票详细信息
@model SaveMultipleRows.Models.invoice
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<hr/>
<div class="form-group">
<label asp-for="invoiceNo" class="control-label"></label>
<input asp-for="invoiceNo" class="form-control" id="invoiceNo"/>
</div>
<div class="form-group">
<label asp-for="supplierName" class="control-label"></label>
<input asp-for="supplierName" class="form-control" id="supplierName"/>
</div>
<table id="tblCustomers" class="table" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th style="width:150px">Item Name</th>
<th style="width:150px">Quantity</th>
<th></th>
</tr>
</thead>
<tbody></tbody>
<tfoot>
<tr>
<td><input type="text" id="txtItemName" /></td>
<td><input type="text" id="txtQuantity" /></td>
<td><input type="button" id="btnAdd" value="Add" /></td>
</tr>
</tfoot>
</table>
<div class="form-group">
<input id="btnCreate" type="submit" value="Create" class="btn btn-default" />
</div>
javascript,使用ajax将数据发布到控制器
@section Scripts
{
<script type="text/javascript">
$("#btnAdd").click(function () {
var ItemName = $("#txtItemName");
var Quantity = $("#txtQuantity");
var tBody = $("#tblCustomers > TBODY")[0];
var row = tBody.insertRow(0);
//Add ItemName cell.
var cell = $(row.insertCell(0));
cell.html(ItemName.val());
//Add Quantity cell.
cell = $(row.insertCell(1));
cell.html(Quantity.val());
//Clear the Textboxes.
ItemName.val("");
Quantity.val("");
});
$("body").on("click", "#btnCreate", function () {
//Loop through the Table rows and build a JSON array of invoiceItems.
var invoiceItems = new Array();
$("#tblCustomers TBODY TR").each(function () {
var row = $(this);
var invoiceItem = {};
invoiceItem.ItemName = row.find("TD").eq(0).html();
invoiceItem.Quantity = row.find("TD").eq(1).html();
invoiceItems.push(invoiceItem);
});
//Populate invoice data
var invoice = {};
invoice.invoiceNo = $("#invoiceNo").val();
invoice.supplierName = $("#supplierName").val();
invoice.invoiceItems = invoiceItems;
invoice.ItemCount = invoiceItems.length; //Count the number of invoiceItems
$.ajax({
type: "POST",
url: "/invoices/CreateInvoice",
data: JSON.stringify(invoice),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert(r + " record(s) inserted.");
}
});
});
</script>
}
控制器中的CreateInvoice方法
[HttpPost]
public async Task<IActionResult> CreateInvoice([FromBody] invoice invoice)
{
_context.Add(invoice);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}