I searched on google and figured a lot of samples are using Single parameter binding to I created the following model and changed the signature of my method to
public class LogInParams
{
public string username { get; set; }
public string password { get; set; }
}
[HttpPost]
public object LogIn(LogInParams par)
Now the above method gets called and par is some object but the properties are null.
Can someone point me in to the right direction here ?
Follow this example.... (Do not forget to mark when the answer is found)
var General = {
HaveTools: $('#chkTools').is(':checked'),
Convicted: $('#chkFelony').is(':checked'),
Citizen: $('#chkCitizen').is(':checked'),
Available: $('#DateAvailable').val(),
Phone_Cell: $('#txtPhoneCell').val(),
Phone_home: $('#txtPhoneHome').val(),
.....
data: JSON.stringify(General),
......
...API Code...
Public Function PostValue(ByVal General As Applications.General) As HttpResponseMessage
..Class....
Public Class General
Public Property HaveTools As Boolean
Public Property Convicted As Boolean
Public Property Citizen As Boolean
Public Property Available As Date
Public Property Phone_Cell As String
Public Property Phone_Home As String
End Class
fahadash
Member
73 Points
80 Posts
Could not bind jSON to parameters
Jan 16, 2013 05:31 PM|LINK
Hi,
I have a simple method with the following signature in my Controller class
[HttpPost] public object LogIn(string username, string password)I am using the following JS to make the call
var params = JSON.stringify({ username: "abc", password: "def" }); $.post($("#serviceurl").val(), params, function (data, textStatus, jqXHR) { alert("Completed : " + textStatus);}, "application/json");I searched on google and figured a lot of samples are using Single parameter binding to I created the following model and changed the signature of my method to
public class LogInParams { public string username { get; set; } public string password { get; set; } } [HttpPost] public object LogIn(LogInParams par)Now the above method gets called and par is some object but the properties are null.
Can someone point me in to the right direction here ?
Thanks
Rion William...
All-Star
27656 Points
4574 Posts
Re: Could not bind jSON to parameters
Jan 16, 2013 05:38 PM|LINK
Your parameters are already in JSON format so you don't need to use the JSON.Stringify() method.
Just pass in your existing params variable like shown below :
var params = { username: "abc", password: "def" }; $.post($("#serviceurl").val(), params, function (data, textStatus, jqXHR) { alert("Completed : " + textStatus); }, "application/json");fahadash
Member
73 Points
80 Posts
Re: Could not bind jSON to parameters
Jan 16, 2013 05:41 PM|LINK
Rion,
Still does'nt work. The username and password properties are still null.
Rion William...
All-Star
27656 Points
4574 Posts
Re: Could not bind jSON to parameters
Jan 16, 2013 05:49 PM|LINK
Can you post some additional code? I created an example on my machine of this same functionality and it seemed to work just fine.
Examples :
Javascript :
<script type='text/javascript'> $(document).ready(function () { var params = { username: "abc", password: "def" }; $.post("/Default/LogIn", params, function (data, textStatus, jqXHR) { alert("Completed : " + textStatus); }, "application/json"); }); </script>Controller Action :
[HttpPost] public object LogIn(string username, string password) { return "Testing"; }stockcer
Member
498 Points
128 Posts
Re: Could not bind jSON to parameters
Jan 16, 2013 05:55 PM|LINK
Follow this example.... (Do not forget to mark when the answer is found)
var General = { HaveTools: $('#chkTools').is(':checked'), Convicted: $('#chkFelony').is(':checked'), Citizen: $('#chkCitizen').is(':checked'), Available: $('#DateAvailable').val(), Phone_Cell: $('#txtPhoneCell').val(), Phone_home: $('#txtPhoneHome').val(), ..... data: JSON.stringify(General), ...... ...API Code... Public Function PostValue(ByVal General As Applications.General) As HttpResponseMessage ..Class.... Public Class General Public Property HaveTools As Boolean Public Property Convicted As Boolean Public Property Citizen As Boolean Public Property Available As Date Public Property Phone_Cell As String Public Property Phone_Home As String End ClassASP.NET Arvixe Liaison
James River Webs, Inc.
fahadash
Member
73 Points
80 Posts
Re: Could not bind jSON to parameters
Jan 16, 2013 05:59 PM|LINK
Here is the *real* scary part. After a few re-compiles it started working...
stockcer
Member
498 Points
128 Posts
Re: Could not bind jSON to parameters
Jan 16, 2013 06:31 PM|LINK
Before you declare victory over the code, did you run it in production?
ASP.NET Arvixe Liaison
James River Webs, Inc.
Rion William...
All-Star
27656 Points
4574 Posts
Re: Could not bind jSON to parameters
Jan 16, 2013 07:04 PM|LINK
It's possible that it could have been some sort of caching issue. I'm glad that you got it working though :)
fahadash
Member
73 Points
80 Posts
Re: Could not bind jSON to parameters
Jan 16, 2013 11:29 PM|LINK
It is not going to production anytime soon. But it will have to pass through "mean" testing efforts.