1

I currently have a ajax request setup to send a "PUT" request to a web api in my mvc 4 project. My request is able to get into the method on the api but the parameter is always null. Any ideas why? I have also check the PUT request as its executed and it does send a string of key/value pairs for each form control. Here is my code:

Web Api method (selection is always null)

public void Put([FromBody]string selection)
{
}

Update:

I forgot I was doing a little debugging of my own. I have confirmed that when you serialize a form the param is named "selection". Please take another look.

Ajax call:

$.ajax({
    type: "PUT",
    url: urlPrefix + "api/file/Manipulate",
    data: $("#frmManipulate").serialize(),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    statusCode: {
        204: function (jqXHR) {
            alert("No file(s)/Directory(s) were selected.");
        }
    }
}).done(function () {
    location.reload();
});
1
  • I don't see a simple type called "selection" in your Ajax call. Commented Dec 4, 2013 at 19:32

2 Answers 2

1

It's null because you aren't passing it:

data: { val1 : "test",  val2 : "test2"}

Try:

data: { selection : "something" }
0

It is null because asp.net web api does not know how to deserialize the { val1 : "test", val2 : "test2"} to a string. You could use a DTO approuch to pass these information to the action, for sample:

in the web api project, add a class like this:

public class InfoDTO
{
    public string val1 { get; set; }
    public string val2 { get; set; } 

    // other properties if you need
}

And change your put action to get a parameter with this type:

public void Put([FromBody]InfoDTO info)
{
   // use 'info' object
}

Your client-side javascript can keep with the same code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.