c# - How to add record well using ASP.NET MVC and KnockoutJS? -


<button class="btn btn-primary" data-bind="click:function(){show('add');}">add</button> 

my purpose adding input record values backend automatically id number. after click button add, show me internal server error. record saved database sometimes. id 0. have set id property nullable. below add function , object. may ideas smart guys? thank much.

    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) }; 

the end post action.

    [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;     } 

when deserialized, parameters controller end-point given "default" value if 1 not specified in data. default value integer in c# 0. integer properties of customermodel 0 unless told not be.

i'm assuming customermodel.id int since didn't include code. if that's correct try making nullable<int> instead. way when value not specified default given null.

the same issue applies customer entity model well. if id property isn't nullable value given 0 default unless entityframework knows it's auto-increment (identity) field.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -