[英]Internal Server Error(500) when adding a record to a table using Odata and Knockout Js in Asp.Net MVC
I am trying to add a record to an SQL table using Knockout Js and OData using the following script. 我正在尝试使用以下脚本使用Knockout Js和OData将记录添加到SQL表中。
Knockout Js 淘汰赛
self.Grade = ko.observable();
self.selectedProduct = ko.observable();
self.add = function (grade) {
var payload = { ProductName: this.selectedProduct(), GradeName:this.Grade(), Locked:false };
$.ajax({
url: '/odata/Grades',
type: 'POST',
data: JSON.stringify(payload),
contentType: 'application/json',
dataType: 'json'
});
}
HTML 的HTML
<td >
<select data-bind="options: $root.productNames, optionsText: 'ProductName', optionsValue: 'ProductName', value: selectedProduct, optionsCaption: 'Product'">
</select></td>
<td>
<input type="text" id="txbInput" placeholder="Grade Name" data-bind="value: Grade"/> </td>
<td>
<input type="button", onclick="ClearFields();" class="btn btn-success" data-bind=" click: add, visible: !loading()", value="Add Grade"/></td>
</tr>
But for some reason when I click on Add Grade
button to add a record it is throwing me an exception as follows 但是由于某些原因,当我单击“
Add Grade
按钮以添加记录时,它向我抛出异常,如下所示
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:57044/odata/Grades
May I know the reason and best solution to this. 请问原因和最佳解决方案。
Figured out myself. 弄清楚自己。 A simple mistake is that I am not inserting a value
Id
column and it can't be NULL
as it is a key. 一个简单的错误是我没有插入值
Id
列,并且它不能为NULL
因为它是键。 I changed the Identity specification
property of the Id
column in SQL table to Yes and it worked. 我将SQL表中
Id
列的Identity specification
属性更改为Yes,它起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.