c# - ASP.Net MVC Web Api Function returning error for entirely different call -


i have following api controller:

public class familycontroller : apicontroller {     applicationdb db = new applicationdb();      public ienumerable<family> getfamilies()     {         return db.families.asnotracking().orderby(n => n.familyname);     }      public family getfamily(int id)     {         return db.families.asnotracking().single(n => n.familyid == id);     } } 

the following javascript file called functions.js

(function () { window.dbapp = window.dbapp || {};  // private: routes var familiesurl = function () { return "/api/family/getfamilies" },     familyurl = function (id) { return "/api/family/getfamily?id=" + id };  // private: ajax helper function ajaxrequest(type, url, data) {     var options = { datatype: "json", contenttype: "application/json", cache: false, type: type, data: ko.tojson(data) }     return $.ajax(url, options); }  // public: methods window.dbapp.db = {     getfamilies: function() {         return ajaxrequest("get",familiesurl());     },     getfamily: function (id) {         return ajaxrequest("get", familyurl(id));     }, };  })(); 

and following javascript on view:

@scripts.render("~/bundles/knockout") @scripts.render("~/bundles/functions")     <script type="text/javascript">     function viewmodel() {         var self = this;         self.families = ko.observablearray([]);          self.init = function () {             dbapp.db.getfamilies()                 .done(function (data) {                     self.families(data);                 });         };          self.init();     }      var model = new viewmodel();     ko.applybindings(model); </script> 

but following error returned: parameters dictionary contains null entry parameter 'id' of non-nullable type 'system.int32' method 'madcapsportal.models.family getfamily(int32)' in 'madcapsportal.controllers.familycontroller'. optional parameter must reference type, nullable type, or declared optional parameter.

it's it's calling wrong function. what's strange page uses following works fine??

var passedid = @viewbag.passedid;     function viewmodel() {         var self = this;         self.family = ko.observable();         self.familyname = ko.observable();          self.init = function() {             dbapp.db.getfamily(passedid)                 .done(function(data){                     self.family(data);                     self.familyname(data.familyname);                                         });         };          self.init();     }      var model = new viewmodel();     ko.applybindings(model); 

you supposed use follwing url's:

  1. for list resource use get /api/family
  2. for resource id use get /api/family/2 or get /api/family?id=2

please read more information on asp.net web api routing:

but basically: url action method mapping determined http method , patterns in action method names , methods' signatures. get /api/family mapped method named get , containing no further parameters.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -