发票类别实体:
public class InvoiceCategory
{
[Key]
Public int InvoiceCategoryID { get; set; }
[Required]
public string CategoryName { get; set; }
}
发票项目实体
public class InvoiceItem
{
[Key]
public int InvoiceItemID { get; set; }
[Required]
public int InvoiceID { get; set; }
[Required]
[Display(Name = "Date Completed")]
[DataType(DataType.Date)]
public DateTime? Date { get; set; }
[StringLength(50)]
public string InvoiceCategory { get; set; }
[Required]
[StringLength(200)]
public string Description { get; set; }
[Required]
[StringLength(20)]
public string Unit { get; set; }
[Required]
[DataType(DataType.Currency)]
[DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:c}")]
public decimal Price { get; set; }
[Required]
[DefaultValue(1)]
public decimal? Quantity { get; set; }
public virtual Invoice Invoice { get; set; }
}
发票类别编辑视图
@model Accounts.Models.InvoiceCategory
<h2>Settings</h2>
<br />
<h4>Invoice Categories</h4>
<hr />
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
@(Html.Kendo().Grid<Accounts.Models.InvoiceCategory>()
.Name("InvoiceCategory")
.ToolBar(tools =>
{
tools.Create().Text("New Category");
})
.Editable(editable => editable.Mode(GridEditMode.InCell).CreateAt(GridInsertRowPosition.Bottom).DisplayDeleteConfirmation(false))
.Columns(columns =>
{
columns.Bound(p => p.InvoiceCategoryID).Hidden().ClientTemplate("#= InvoiceCategoryID #" +
"<input type='hidden' name='[#= index(data)#].InvoiceCategoryID' value='#= InvoiceCategoryID #'/>"
);
columns.Bound(p => p.CategoryName).ClientTemplate("#= CategoryName #" +
"<input type='hidden' name='[#= index(data)#].CategoryName' value='#= CategoryName #'/>"
);
columns.Template("<a class='btn btn-danger btn-sm k-grid-delete'><span class='glyphicon glyphicon-trash'></span> </a>").Width(50);
})
.DataSource(dataSource => dataSource.Ajax()
.Model(model =>
{
model.Id(p => p.InvoiceCategoryID);
model.Field(p => p.InvoiceCategoryID).Editable(false);
})
.Read(read => read.Action("InvoiceCategories_Read", "Settings"))
.ServerOperation(false)
).Deferred()
)
<br />
<button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-floppy-disk" style="vertical-align:middle;margin-top:-5px"></span> Save</button>
</form>
@section Scripts {
@Html.Kendo().DeferredScripts()
<script>
function index(dataItem) {
var data = $("#InvoiceCategory").data("kendoGrid").dataSource.data();
return data.indexOf(dataItem);
}
</script>
<style>
.k-grid {
width: 280px;
}
</style>
}
控制器发票类别更新方法:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(IEnumerable<InvoiceCategory> invoiceCategory)
{
if (ModelState.IsValid)
{
try
{
_context.InvoiceCategory.UpdateRange(invoiceCategory);
_context.SaveChangesAsync();
}
catch (Exception ex)
{
...
}
}
}
视图中的值正在更改,并且更改将发送到控制器的Edit方法,但是由于某些原因,使用save方法不会将更改公布到数据库。
实体框架是否不支持像我一样没有整数标识列的查找表?或者我做了一些愚蠢的事情?
答案 0 :(得分:5)
基本上,您要做的是更新名为InvoiceCategory
的表的主键。无论如何,对于代码的上下文,如果您在数据库的上下文中考虑它,这没有意义。主键是记录的唯一标识符。您应该无法更新它。
作为旁注,每当您使用async
时,您应至少使用一次await
。否则你的方法将同步执行。在你的情况下,遗漏等待是这样的:
await _context.SaveChangesAsync();
<强>更新强>
我认为你需要的是以下几点:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(IEnumerable<InvoiceCategory> invoiceCategories)
{
if (ModelState.IsValid)
{
try
{
foreach(var invoiceCategory in invoiceCategories)
{
if(invoiceCategory.InvoiceCategoryID == 0)
{
_context.InvoiceCategory.Add(invoiceCategory);
}
else
{
_context.InvoiceCategory.Update(invoiceCategory);
}
}
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
...
}
}
}