Casey Crookston
Casey Crookston

Reputation: 13965

knockout: Trouble removing a newly added item

I'm trying to learn how to use KnockOut... never have before and I've been thrown into the fire on a site already using it. Everything here works fine:

        function MasterViewModel() {
            var self = this;
            self.Supervisors = ko.mapping.fromJS(@Html.Raw(JsonConvert.SerializeObject(Model.Supervisors)));

            self.AddSupervisor = function(request) {
                var request = new Supervisor({
                    FullName:  $('#SupervisorId option:selected').text(),
                    SupervisorId: $('#SupervisorId option:selected').val()
                });
                self.Supervisors.push(request);
                // do server side call here
            }

            self.RemoveSupervisor = function(request) {
                if (request.SupervisorID() > 0)
                {
                    self.Supervisors.remove(request);
                     // do server side call here
                }
            }
        }

Well. Everything almost works fine:

But, when I try to remove an item that I just added, I get this:

Uncaught TypeError: request.SupervisorID is not a function

enter image description here

SupervisorId is a dropdown. The AddSupervisor call is made from a button. I can show the HTML if needed. Also, although I may not need this if:

if (request.SupervisorID() > 0)

Even without it, I am going to need the ID of the supervisor that was added.

Upvotes: 1

Views: 24

Answers (1)

James Thorpe
James Thorpe

Reputation: 32222

I'm guessing the server side isn't case-sensitive, and is loading data with SupervisorID. When you add a new one, you're creating it with SupervisorId (lowercase d). The server must be accepting of that. JavaScript isn't.

You need to either change the newly created users to use SupervisorID, or have the RemoveSupervisor function use SupervisorId - whichever change makes more sense in your overall structure.

Upvotes: 1

Related Questions