MVVM & ModelState (form submit)

3 posts, 1 answers
  1. Andrea
    Andrea avatar
    12 posts
    Member since:
    Jan 2014

    Posted 03 May 2014 Link to this post

    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:

    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();

        url: '@(Url.Action("Action", "Controller"))',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(o),
        success: function (response) {
        error: function (e) {

    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

  2. Answer
    Petur Subev
    Petur Subev avatar
    1882 posts

    Posted 07 May 2014 Link to this post

    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
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Andrea
    Andrea avatar
    12 posts
    Member since:
    Jan 2014

    Posted 07 May 2014 in reply to Petur Subev Link to this post 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...
    Andrea Bioli
Back to Top