Jquery Ajax does not display Object in the PUT method, using . net mvc

Asked

Viewed 24 times

1

I own the Object var obj = { nome: "Felipe", idade: 23 }

I created an ajax call by jquery like this:

$.ajax({
            url: 'ConfigCode/UpdateOptionsStatus', // route controller na web
            type: 'PUT',
            data: JSON.stringify(obj),
            contentType: "application/json; charset=utf-8",
            dataType: "json"
});

In the Web Controller ConfigCode I created the method UpdateOptionsStatus:

        [HttpPut]
        public ActionResult UpdateOptionsStatus(object objectToSend)
        {
            RestHelper.Put(objectToSend, $"ConfigCode/Update");

            return new EmptyResult();
        }

What in the objectToSend in fact carries a {object}

On the controller ConfigCode API, I created the method Update to receive it:

        [HttpPut, IgnorePermission]
        public HttpResponseMessage Update(object optionsToUpdate)
        {
            (...)

            if (result == null)
                return NotFound();

            return Ok(result);
        }

But the {Object} sent. An empty tag arrives {{}} in optionsToUpdate

Where is the error?

  • 1

    why instead of receiving a object, does not create a model class and takes it as parameter? are only 2 very simple properties

  • yes it worked. I did an Update(List<Viewmodelcriada> optionsToUpdate) and changed the object to an object array with the Viewmodel properties.

  • good Luke, serializer/deserializer works best with defined types, Object, Dynamic and the like are complicated for this :)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.