This is a migrated thread and some comments may be shown as answers.

MVVM & ModelState (form submit)

3 Answers 182 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Andrea
Top achievements
Rank 1
Andrea asked on 03 May 2014, 03:16 PM
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

3 Answers, 1 is accepted

Sort by
0
Accepted
Petur Subev
Telerik team
answered on 07 May 2014, 07:46 AM
Hello Andrea,

Thank you for the detailed explanation. The is a feature designed to work completely on the client side (it is great for SPA) and it is not related to any server side technology - MVC PHP etc. 

So If you want to take advantage of the MVC framework features such as ModelState support you should consider using input widget directly, so they are submitted along with the form.

There is nothing else we can suggest you to leverage your MVC development with the MVVM support of Kendo.

Kind Regards,
Petur Subev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Andrea
Top achievements
Rank 1
answered on 07 May 2014, 08:08 AM
...so are you telling that ASP.NET MVC technology and Kendo MVVM technology are totally alternative each other?
Ok, I think this is a very important thing to keep in mind, for developers...
Thanks.
Andrea Bioli
0
Petur Subev
Telerik team
answered on 07 May 2014, 01:30 PM
Hello again Andrea,

No, the MVVM framework is a client side feature, it works on the client side and it is not intended to leverage your MVC development, however as you explained you can still use it to send the updated model ($.post) to the server where you can save the changes that were made on the client side.

Kind Regards,
Petur Subev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
MVVM
Asked by
Andrea
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Andrea
Top achievements
Rank 1
Share this question
or