Observable objects (models) are a really good piece of software. I don't know if I'm missing something, but maybe they lack something in functionalities...
I try to explain it with an example.
Let's suppose I want to insert/edit a complex (db) object. I have a controller that initializes a new instance of a (ASP.NET MVC) ViewModel object, passing it to the View, and sending the resulting HTML to the client.
In the client, I want to use the good (Telerik) MVVM support (I find very useful its services) so I initialize a local js object with something like:
kendo.init(document.body);
model = @(Html.Raw(new JavaScriptSerializer().Serialize(Model)));
vm = kendo.observable(model);
kendo.bind(document.body, vm);
That means that starting with my original C# object I create an equivalent js (observable) object that contains the same values, that I can use to bind local controls to it (examples for this are good enough in the site).
The user edits data, writes what he wants, and then, at last, needs to send these data to the server.
I know that I can do something like this:
var o = vm.toJSON();
$.post({
url: '@(Url.Action("Action", "Controller"))',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(o),
success: function (response) {
alert('OK');
},
error: function (e) {
alert('KO');
}
});
In this way, I can have on the server (letting infrastructure recreating it) the (C#) ViewModel, so it's really that easy to use that data: it's an instance of the ViewModel I sent to the View, so MVVM support is totally transparent (and this is a wonderful advantage).
The problem is: this is a post, so there is not a page refresh: specifically, I cannot use the ModelState support to show errors...
In the server I have something like:
if (<something wrong>)
ModelState.AddModelError("Field", "Bla Bla");
but to let ASP.NET MVC infrastructure handle this correctly, I would have to submit the 'normal' form: even in that case, what I would like to submit, as data, would be the serialized version of the MVVM observable object, and not the form fields (maybe I have much more information in the object, than what I display/edit with controls...). Without this support, I don't know if the whole infrastructure is really usable, so I think there must be a key I didn't find...
Anyone ever thought about this? What am I missing?
Thanks as always in advance for your wonderful support.
Andrea Bioli
I try to explain it with an example.
Let's suppose I want to insert/edit a complex (db) object. I have a controller that initializes a new instance of a (ASP.NET MVC) ViewModel object, passing it to the View, and sending the resulting HTML to the client.
In the client, I want to use the good (Telerik) MVVM support (I find very useful its services) so I initialize a local js object with something like:
kendo.init(document.body);
model = @(Html.Raw(new JavaScriptSerializer().Serialize(Model)));
vm = kendo.observable(model);
kendo.bind(document.body, vm);
That means that starting with my original C# object I create an equivalent js (observable) object that contains the same values, that I can use to bind local controls to it (examples for this are good enough in the site).
The user edits data, writes what he wants, and then, at last, needs to send these data to the server.
I know that I can do something like this:
var o = vm.toJSON();
$.post({
url: '@(Url.Action("Action", "Controller"))',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(o),
success: function (response) {
alert('OK');
},
error: function (e) {
alert('KO');
}
});
In this way, I can have on the server (letting infrastructure recreating it) the (C#) ViewModel, so it's really that easy to use that data: it's an instance of the ViewModel I sent to the View, so MVVM support is totally transparent (and this is a wonderful advantage).
The problem is: this is a post, so there is not a page refresh: specifically, I cannot use the ModelState support to show errors...
In the server I have something like:
if (<something wrong>)
ModelState.AddModelError("Field", "Bla Bla");
but to let ASP.NET MVC infrastructure handle this correctly, I would have to submit the 'normal' form: even in that case, what I would like to submit, as data, would be the serialized version of the MVVM observable object, and not the form fields (maybe I have much more information in the object, than what I display/edit with controls...). Without this support, I don't know if the whole infrastructure is really usable, so I think there must be a key I didn't find...
Anyone ever thought about this? What am I missing?
Thanks as always in advance for your wonderful support.
Andrea Bioli