I just wanted to know, what is the elegant way of populating the observable properties after posting json to the server.
my js
var vm = (function() {
var commit = function(item) {
var model = {
Name: item.name(),
IsActive: item.is_active()
};
$.post('/to/server/Post', model);
.done(function(d) {
item.id(d.Id);
});
}
}());
in my server
public JsonResult Post(itemVm model)
{
var item = new Item
{
Name = model.Name,
IsActive = model.IsActive
};
// do saving here and commit to database
model.id = item.Id;
return Json(model, JsonResultBehavior.DenyGet);
}
While above snippet will work. I find it hard to maintain it this way, is there an elegant or another way of doing this? I need the returned Id for update later on.
Btw, I'm using ASP.NET MVC 5, KnockoutJs
Any help would be much appreciated. Thanks