简体   繁体   中英

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

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

<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

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. I changed the Identity specification property of the Id column in SQL table to Yes and it worked.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM