Bryan Dellinger
Bryan Dellinger

Reputation: 5294

How to post part of the viewmodel to a Web API controller

I have a VB Web API app.

I have a VB class/model like so.

Imports System.ComponentModel.DataAnnotations
Imports System.Web.Http
Public Class MSLDestinationInput
    <HttpBindRequired>
    <Required>
    Public Property ShpmntCntrlNbr() As String
        Get
            Return m_ShpmntCntrlNbr
        End Get
        Set(value As String)
            m_ShpmntCntrlNbr = value
        End Set
    End Property
    Private m_ShpmntCntrlNbr As String
End Class

This is the controller:

   Public Async Function GeneratePDF(data As MSLDestinationInput) As Task(Of IHttpActionResult)
             If Not ModelState.IsValid Then
            Return BadRequest(ModelState)
        End If
        Dim oMSLOutput As New MSLOutput
        oMSLOutput.url = "api/PrintGenerateMSL"
        Return Ok(oMSLOutput)
    End Function

I am posting to the controller using jQuery.ajax with this parameters:

 url: 'api/PrintGenerateMSL',
 data: ko.toJSON(self),
 type: "POST",

and everything is working well. However I don't really need to send the entire knockout model. I just need to send some of the properties. I've tried to send this data:

data: {ShpmntCntrlNbr : self.ShpmntCntrlNbr() };

instead of ko.toJSON(self). When the request reaches my controller, I find the parmeter data is empty. How can I send only the required data to my controller instead of the whole ko view model?

Upvotes: 0

Views: 362

Answers (2)

JotaBe
JotaBe

Reputation: 39014

You need to stringify the data. One way to do it is by using JSON.stringify, as you've done.

Most, but not all browsers, include the JSON manipulation functions. The problem is that if someones tries to use your application in a browser that doesn't have this methods, it will crash. Or you'll have to suplly a polyfill.

The good news is that you don't need to worry about it if you use ko.toJSON. In fact ko.toJSON does two things:

  • unwraps all the observables, if they exist
  • convert to JSON, by using JSON.stringify

That means that both of this options would work fine:

data: ko.ToJSON({ShpmntCntrlNbr : self.ShpmntCntrlNbr() })
data: ko.ToJSON({ShpmntCntrlNbr : self.ShpmntCntrlNbr })

Note that the property on the second one would be automatically unwrapped. If you took a piece of your viewmodel which is an object tree that includes some observable properties at any level, ko would also unwrap them automatically.

And, best of all, if the browser does not implement JSON.stringify, ko provieds its own implementation.

Upvotes: 1

Bryan Dellinger
Bryan Dellinger

Reputation: 5294

yes stringify took care of it. it is working now with.

data:  JSON.stringify({ShpmntCntrlNbr : self.ShpmntCntrlNbr() }),

Upvotes: 1

Related Questions