breezejs与保存捆绑问题 [英] breezejs issues with the save bundle
问题描述
与breezejs进出口工作,和我的应用程序的服务器端code是.NET。
im working with breezejs, and the server-side code of my app is .net.
在我的意见(客户端),我想补充和实体的话,我想保存它。让我们假设一个实体是这样的:
in my views (client side), i want to add and entity then i want to save it. Lets assume that an entity is like this :
{
"Id": 1,
"Name": "someName",
"CreatedDate": "1900-01-01T05:00:00Z",
"UpdatedDate": "1900-01-01T05:00:00Z",
"CreatedBy": null,
"UpdatedBy": null,
"RowVersion": 0,
etc ...
}
}
我想设置 CreatedDate
UpdatedDate
CreatedBy $ C $的值C>和
UpdatedBy
,我能做到这一点当然是使用JavaScript的,但我不希望客户端利用这些样的东西照顾。
i want to set the values of CreatedDate
UpdatedDate
CreatedBy
and UpdatedBy
, i can do that using javascript of course, BUT i dont want the client to take care of those kind of things.
我的微风控制器在哪里住这个功能是这样的:
my breeze controller where lives this function is like this:
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
return _contextProvider.SaveChanges(saveBundle);
}
为u可以看到saveBundle是JObject,当我调试我看到saveBundle是这样的:
as u can see the saveBundle is a JObject, when i debug i see the saveBundle like this:
{
"entities": [
{
"Id": 1,
"Name": "someName",
"CreatedDate": "1900-01-01T05:00:00Z",
"UpdatedDate": "1900-01-01T05:00:00Z",
"CreatedBy": null,
"UpdatedBy": null,
"RowVersion": 0,
etc ...
}
}
}
],
"saveOptions": {}
}
我怎样才能改变 CreatedDate
UpdatedDate
CreatedBy $ C $的值C>和
UpdatedBy
在saveBundle保存前将提交???
How can i change the values of CreatedDate
UpdatedDate
CreatedBy
and UpdatedBy
in the saveBundle before the save is commited ???
这是与对象作为亲preTY阵列的JObject,我可以操纵的Json使用JavaScript,我怎么能做到这一点与.net ???
this is a JObject with an array of objects as proprety, i can manipulate Json with javascript, how can i do it with .Net ???
多谢了。
推荐答案
由于周杰伦Traband后,我终于找到了一种方法在保存前更改的实体。
Thanks to Jay Traband post I finally found a way to make changes to an entity before saving it.
我重载 BeforeSaveEntity
是这样的:
protected override bool BeforeSaveEntity(EntityInfo entityInfo) {
// Return false if don´t want to save the entity
var entityType = entityInfo.Entity.GetType();
if (entityInfo.Entity.GetType() == typeof(MyEntityTypeModel))
{
if (entityInfo.EntityState == EntityState.Added)
// It can be 'Modified' or 'Deleted'
{
var MyModel = entityInfo.Entity as MyEntityTypeModel;
MyModel.CreatedDate = DateTime.Now;
MyModel.UpdatedDate = DateTime.Now;
string username = Membership.GetUser().UserName;
MyModel.CreatedBy = username;
MyModel.UpdatedBy = username;
}
}
return true;
}
感谢了很多,我希望这将帮助别人一天。
Thanks a lot, and I hope this will help someone someday.
这篇关于breezejs与保存捆绑问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!