Hi I have agularjs resources below that is working fine.
嗨,我在下面的agularjs资源工作正常。
return {
formDis: $resource('/api/pDisc/:id', { id: '@id' }, { update: { method: 'POST' } })
};
angularjs cotroller using the resource is
使用资源的angularjs cotroller是
$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
接收它的MVC Api是
// 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
当我运行它时,我的api中出现错误
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.
我收到此错误,因为api正在接收空对象。在角度控制器中,我可以看到控制台中的对象从表单中正确传递。一旦将对象提交给资源并从该对象提交到api,就会出现错误,最终将其作为空对象。请让我知道如何解决它。
1 个解决方案
#1
0
Based on your comment:
根据您的评论:
I can see the right data being {"pro_id":"221","message":"sdsfsd" pDis_id:""}
我可以看到正确的数据是{“pro_id”:“221”,“message”:“sdsfsd”pDis_id:“”}
My best guess is that the model binder is not able to convert to the pDis type.
我最好的猜测是模型绑定器无法转换为pDis类型。
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.
我注意到的一件事是你的属性pDis_id是一个int(不可为空),你传递给它pDis_id:“”。我认为模型绑定器在这种情况下不知道该怎么做。
Try supplying an integer value for pDis_id, or not supplying it at all.
尝试为pDis_id提供整数值,或者根本不提供它。
#1
0
Based on your comment:
根据您的评论:
I can see the right data being {"pro_id":"221","message":"sdsfsd" pDis_id:""}
我可以看到正确的数据是{“pro_id”:“221”,“message”:“sdsfsd”pDis_id:“”}
My best guess is that the model binder is not able to convert to the pDis type.
我最好的猜测是模型绑定器无法转换为pDis类型。
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.
我注意到的一件事是你的属性pDis_id是一个int(不可为空),你传递给它pDis_id:“”。我认为模型绑定器在这种情况下不知道该怎么做。
Try supplying an integer value for pDis_id, or not supplying it at all.
尝试为pDis_id提供整数值,或者根本不提供它。