Alias web api request model - ajax

I am trying to use ajax call on asp.net web api where i want to alias the request model property name.
However, when i post the value back to the server and receive it as request, it does not work as expected. what i intend to achieve is as follow:
When i pass the data valueA back to web api, it will mapped to MemberName. Not sure where i did wrong.
THis is what something i expect.
$.ajax({
url: '..',
dataType: 'json',
type: 'POST',
data: { 'valueA': 'ABC' },
success: {}
})
public class MemberProfile {
[JSONProperty('valueA')]
public string MemberID { get; set; }
}
[HttpPost]
public HttpResponseMessage GetMemberProfile(MemberProfile request)
{
}

You should change you JavaScript code to :
$.ajax({
url: '..',
contentType:'application/json', //here
dataType: 'json',
type: 'POST',
data: JSON.stringify({ valueA: "ABC" }), //here
success: {}
})

Related

Ajax sending variable value to MVC controller

I am trying to send a value from jQuery using ajax call and fetch the value in the controller. Below is what I am trying, but the value of A is always null. Can someone help me where I am going wrong.
$.ajax({
type: "POST", //The type of Method being used (GET, SET, POST, etc.)
url: '/Controller1/Index',
contentType: 'application/json; charset=utf-8',
data: { A: "true" }, //Your data to pass to your Controller action
dataType: "json",
success: function (result) {
alert(response.responseText);
alert("Done!");
}
});
C# code
[HttpPost]
[ActionName("Index")]
public ActionResult IndexPost(FilterRule queryBuilder, BootstrapTableQueryType bootstrapTable, string A)
{
string B = A;
}

ASP.NET MVC Ajax: refuse to send long string

I am trying to send long string through Ajax. When the "note" string is short, it goes through fine. When it is more than a thousand characters or so, it silently fails, the action method is not called.
Ajax call:
$.ajax({
url: '#Url.Action("SaveSchoolNote")',
type: 'POST',
dataType: 'json',
data: { schoolId: schoolId, note: note },
cache: false,
success: function (data) {
}
});
Action:
public ActionResult SaveSchoolNote(int schoolId, string note)
{
...
}
Please help!

Trying to send multiple parameters to ASP.NET webAPI post method results server error 500

Iam trying to send multiple parameters with complex datatype to POST method in WebAPI but it fails with 500 server error. I will be greatful if somebody help me finding what is missing ?
Ajax:
var x={}
var y={}
$.ajax({
cache: false,
type: "POST",
data: JSON.stringify({xDto:x,yDto:y}),
url: "/api/Info/PostInfo",
dataType: 'json',
contentType: "application/json;charset=utf-8",
success: function(data) {
}
error: function(data) {
alert(JSON.stringify(data));
}
})
Action:
public IHttpActionResult PostInfo(InfoDto xDto,InfoDto yDto)
{
//post xDto and yDto to db
}
I would change the API Parameter to an InfoDto array and retrieve it from the body:
public IHttpActionResult PostInfo([FromBody]InfoDto[] xDtos)
{
}
You also have to change your JavaScript to something like this:
data: JSON.stringify([x, y])

Ajax : Can't bind multiple parameters

Hey I am trying to bind multiple parameters in ajax post request but I am getting the following error.
Can't bind multiple parameters to the request's content.
Here is my code
MVC Api Controller Side
public void Post(Email email, PInformation pInformation)
{
//do something.
}
Ajax Call
var mail = { mail: 'myemail', Password: 'pass' };
var ppInformation = { FirstName: 'James', LastName: 'Jones' };
var datum = { email: mail, pInformation: ppInformation };
$.ajax({
url: 'url',
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: JSON.stringify(datum),
success: function (result) {
}
});
You need to use a composite, so:
$.ajax({
...
data: {form: datum},
});
Your controller method:
public String controlerMethod(#RequestBody FormData form){
...
}
Your form model:
class FormData {
Email email;
PInformation pInformation;
// getters & setters
}
This solution should work, but I can't say that there is some sort of multiple RequestBody available for POST method.

.NET MVC 4 API Controller not getting AJAX data?

I have a simple ajax call that is not sending data correctly. What am I doing wrong? Here is the AJAX:
$.ajax({
url: '/api/Chat/New',
type: 'POST',
dataType: 'json',
data: { id : 10}
});
and here is my controller:
public class ChatController : BaseApiController
{
//
// GET: /Chat/
[HttpPost]
public int New(int id = -1)
{
return id;
}
}
BaseApiController is my own controller that has DB context in it and it inherits from ApiController. I cannot get data to send across the wire at all. New() just returns -1 every time.
try removing the data type from your ajax call. Something like this
$.ajax({
url: '#Url.Action("New", "Chat")',
type: 'post',
cache: false,
async: true,
data: { id: 10 },
success: function(result){
// do something if you want like alert('successful!');
}
});
try this
$.ajax({
url: '/api/Chat/New',
type: 'POST',
dataType: 'json',
data: JSON.stringify({ id : 10})
});
Please take a look at the answer of the following post
http://forums.asp.net/t/1939759.aspx?Simple+post+to+Web+Api
Basically, Web API Post accept only one parameter, it can be a primitive type or a complex object.
Change your ajax request to the following
$.ajax({
url: '/api/Chat/New',
type: 'POST',
data: { '' : 10}
});
Then change your controller as follows
[HttpPost]
public int New([FromBody]int id = -1)
{
return id;
}
If it's a complex object like a ViewModel, you don't need to use FromBody
You can read more about why this is required by reading "Using[FromBody]" section in the following article.
http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

Resources