Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello, I have tried to post json data from angularjs $http post to asp.net mvc controller here is json data example
 
 
 var postdata =
                                 {

                                     Email: "test1@mail.com",
                                     selectedanswer: {0: 3, 1: 2, 2: 0, 3: 3, 4: 1}                       
                                 };
 
 
 angular js code 
 
$http({
                url: "/page/PostFileWithData",
                headers: {
                    'Content-Type': 'application/json; charset=utf-8'
                },
                method: "POST",
                dataType: "json",
                traditional: true,
                data: JSON.stringify(postdata)
            }).success(function (data) {
                console.log(data);
            }).error(function (data) {
                console.log('fail');
            });
 
 controller 

public JsonResult PostFileWithData(UserModel userdata)
        { 
             UserModel udata = new UserModel
            {
                Email = userdata.Email  
                selectedanswer = userdata.selectedanswer
            };
return Json(udata, JsonRequestBehavior.AllowGet);

         }
 
 
 
 my Modal
 
 
 public class UserModel
        {
        
            public string Email { get; set; }
            public string selectedanswer { get; set; }



        }
 
 
now problem is that I am getting email field ok  in console.log but selectedanswer is null
 
 
like this
 
{Email: "test1@gmail.com", selectedanswer: null} 
 
 
can you please help me at this point? 


What I have tried:

hello, I have tried to post json data from angularjs $http post to asp.net mvc controller here is json data example
 
 
 var postdata =
                                 {

                                     Email: "test1@mail.com",
                                     selectedanswer: {0: 3, 1: 2, 2: 0, 3: 3, 4: 1}                       
                                 };
 
 
 angular js code 
 
$http({
                url: "/page/PostFileWithData",
                headers: {
                    'Content-Type': 'application/json; charset=utf-8'
                },
                method: "POST",
                dataType: "json",
                traditional: true,
                data: JSON.stringify(postdata)
            }).success(function (data) {
                console.log(data);
            }).error(function (data) {
                console.log('fail');
            });
 
 controller 

public JsonResult PostFileWithData(UserModel userdata)
        { 
             UserModel udata = new UserModel
            {
                Email = userdata.Email  
                selectedanswer = userdata.selectedanswer
            };
return Json(udata, JsonRequestBehavior.AllowGet);

         }
 
 
 
 my Modal
 
 
 public class UserModel
        {
        
            public string Email { get; set; }
            public string selectedanswer { get; set; }



        }
 
 
now problem is that I am getting email field ok  in console.log but selectedanswer is null
 
 
like this
 
{Email: "test1@gmail.com", selectedanswer: null} 
 
 
can you please help me at this point? 
Posted
Comments
Kornfeld Eliyahu Peter 6-May-18 14:52pm    
The problem that selectedanswer is not a string...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900