Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi I have agularjs resources below that is working fine.

return {
        formDis: $resource('/api/pDisc/:id', { id: '@id' }, { update: { method: 'POST' } })
    };

angularjs cotroller using the resource is

$scope.submit = function (form) {
        console.log(form.dis.length);
        console.log(form.dis);
        for (var i = 0; i < form.dis.length; i++) {       
            pRepository.formDis.update(form.dis[i], function () {
                {
                    alert("Saved");
                }
            });      
       };
    };

WebConfig is

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

MVC Api that is recieving it is

// POST api/projDisc
        public HttpResponseMessage Postproject_discussion(pDis pDis)
        {          
                db.project_discussion.Add(pDis);
                db.SaveChanges();          
        }

Class is

 [DataContract(IsReference = false)]
    [Serializable]
    public partial class pDis
    {
        public int pDis_id { get; set; }
        public int pro_id { get; set; }
        public string message {get; set;}

        public virtual pro pro { get; set; }
        public virtual people people { get; set; }
    }
}

When I run it I get error in my api at

db.project_discussion.Add(pDis); 

Error is

  An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll but was not handled in user code

I get this error because api is receiving empty object. In angular controller I can see the objects in console being passed properly from form. Once the object is submitted to the resource and from it to api there is something wrong that it ends up as empty object. Please let me know how to fix it.

share|improve this question
    
Can you use the browser debug tools and see what is being sent to the server? –  Phil Sandler 4 hours ago
    
In browser tool I see, POST localhost:60482/api/projDis Posted data is {"pro_id":"221","message":"sdsfsd" pDis_id:""} The data is being submitted fine from controller by the time it arrives in api it is just empty –  J. Davidson 4 hours ago
add comment

1 Answer

up vote 0 down vote accepted

Based on your comment:

I can see the right data being {"pro_id":"221","message":"sdsfsd" pDis_id:""}

My best guess is that the model binder is not able to convert to the pDis type.

One thing I notice is that your property pDis_id is an int (non-nullable), and you are passing it pDis_id:"". I think the model binder will not know what to do in that case.

Try supplying an integer value for pDis_id, or not supplying it at all.

share|improve this answer
    
I did that still same problem. I looked further along the same line. Made some changes now I get the records but just empty list. I believe the real problem lies in the entity, starting a new question for that. Thanks –  J. Davidson 1 hour ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.