2

I'm starting MVC and I understand the difference between models and ViewModels, thanks to this post:

http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.net-mvc-applications

However I showed this to my colleague and am now under the impression that this is an alternative to using knockout to bind views to models. Can someone explain this? Does knockout binding do the same thing as defining a ViewModel class with properties?

Thank you!

2
  • 2
    Knockout uses a client-side view-model; MVC uses a server-side view-model.
    – SLaks
    Commented Jan 17, 2013 at 18:32
  • So it's either one or the other? How do you decide which to use?
    – RobVious
    Commented Jan 17, 2013 at 18:53

3 Answers 3

5

In convention you can use Knockout view-model is as client-side and MVC view-model is as server-side.

Your question : How do you decide which to use ?

Answer : You can use both as parallel.That means for your single cshtml page can have a knockout view-model is as below.It contains both properties and functions as a unit.This view-model for the behavior what you need for the client side functionality.

    //This is a simple Viewmodel
    //JavaScript that defines the data and behavior of your UI
    function AppViewModel() {
        var self = this;
        self.firstName = ko.observable();
        self.lastName = ko.observable();

        self.fullName = ko.computed(function () {
            return self.firstName() + " " + self.lastName();
        });

        self.capitalizeLastName = function () {
            var currentVal = self.lastName();//Read the current value
            self.lastName(currentVal.toUpperCase());//Write back a modified value
        };
    }

But for show server side behaviors you can use more complex (or more properties) with the MVC view-model.That is for bring data from database and show those data in your view.

Conclusion: So you can use both view-models simultaneously when working with MVC.

Important Note : But if you need to use single view-model for both scenarios then you can do that thing also.For that you have to use KnockoutJS Mapping plugin.

It can be used like below.

<script src="~/Scripts/knockout.mapping-latest.js"></script>
 <script type="text/javascript">
        $(function() {
            var viewModel = ko.mapping.fromJS(@Html.Raw(Model.ToJson()));
            ko.applyBindings(viewModel);
          });
</script> 

You can get more details about this from Loading KnockoutJS View Models from ASP.Net MVC, for faster page loads

If you need to know more about Knockout check learn.knockoutjs

I hope this will help to you.

3

Knockout view models are client side and MVC is server side, that is the big difference.

Knockout allows you to create single page applications, and encapsulates logic on the client side.

ViewModels in MVC only works server side for rendering the page and handle postbacks.

0

Xharze answer is valid, but...

If you need to add view related logic to your Business entities that can not be done Client side then you need to have server side View models. If not they become redundant.

There is still a difference to classic MVC, these server side models will not be used to render server side content, they will be exposed to the client side KO engine using REST

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.