【发布时间】:2018-01-22 20:32:55
【问题描述】:
<button class="btn btn-primary" data-bind="click:function(){show('add');}">Add</button>
我的目的是将我的输入记录值添加到带有自动 ID 号的后端。但是在我单击添加按钮后,显示内部服务器错误。该记录有时会保存到数据库中。但 Id 为 0。我已经将 Id 属性设置为 Nullable。下面是我的 Add 函数和对象。我可以从你们聪明的人那里得到一些想法吗?非常感谢。
self.addCustomer = function () {
debugger;
var cus = { Name : self.cusName(), Address: self.cusAddress()}
var url = "/Customer/AddCustomer";
$.ajax({
type: "POST",
url: url,
data: cus,
success: function (data) {
alert("Insert Successful!");
GetCustomers();
$('#AddCustomer').modal('hide');
},
error: function (error) {
alert(error.statusText);
}
});
};
self.customers = ko.observableArray([]);
GetCustomers();
function GetCustomers() {
$.ajax({
type: "GET",
url: "/Customer/GetCustomer",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (data) {
self.customers(data);
},
error: function (error) {
alert(error.statusText);
}
});
}
self.getSelected = function (cus) {
self.cusId(cus.Id),
self.cusName(cus.Name),
self.cusAddress(cus.Address)
};
后端发布操作。
[HttpPost]
public ActionResult AddCustomer(CustomerModel customer)
{
var cus = new Customer
{
//Id = customer.Id,
Name = customer.Name,
Address = customer.Address
};
customerContext.Customers.Add(cus);
customerContext.SaveChanges();
return null;
}
【问题讨论】:
标签: c# asp.net-mvc knockout.js