【发布时间】:2017-03-08 02:49:32
【问题描述】:
请参考这张图:
此表数据是使用 Entity Framework 获取的。当我双击EmailID 并更改它时,数据将自动更新到 SQL Server。
这里是关于更新的 web api 代码:
// PUT: api/Subscriber/5
public void Put(tbl_Subscribers sub)
{
if (ModelState.IsValid)
{
myEntity.Entry(sub).State = EntityState.Modified;
try
{
myEntity.SaveChanges();
}
catch (Exception)
{
throw;
}
}
}
这段代码运行时,修改后的emailID已经更新到SQL Server,不需要任何sql状态,不需要ID,如何更新代码?
AngularJS 控制器:
$scope.updSubscriber = function (sub, eve) {
sub.MailID = eve.currentTarget.innerText;//get EmailID
var upd = APIService.updateSubscriber(sub);//update EmailID
upd.then(function (d) {
getAll();
}, function (error) {
console.log('Oops! Something went wrong while updating the data.')
})
};
角度服务:
this.updateSubscriber = function (sub) {
return $http({
method: 'put',
data: sub,
url: 'api/Subscriber'
});
}
希望有人能帮我解释一下。
【问题讨论】:
-
how it update the code -
请参考截图。
-
从
put方法返回更新后的模型,然后从结果中以角度更新您的模型。 -
不需要任何sql状态嗯...我不知道你说的sql状态是什么意思,但是你设置了
sub' s entity 状态为Modified. -
是的,你是对的,我们不需要任何sql状态,数据已经绑定。所以 EF 会自动识别它们。
标签: angularjs entity-framework asp.net-web-api