Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout.js with ASP.NET MVC and Razor

Can someone explain to me why would I use Knockout.js with ASP.NET MVC? Doing some research it looks like the major usage of knockout.js is for the data-bindings. But why would I need to use knockout.js when I can bind elements with strongly-typed Views and using Razor?

Instead of the Knockout way:

<td data-bind="text: Id"></td>

Can't I just do this with Razor?

<td>@Model.Id</td>

I see that knockout is now included in MVC 4 but not sure how I can utilize it.

Sorry if this has been asked before but I did some searches on SO but I couldn't find anything that was clear to me.

like image 747
duyn9uyen Avatar asked Dec 19 '22 18:12

duyn9uyen


1 Answers

Basically MVVM js frameworks such as knockoutjs are intended to be used in Single Page Applications where you have a minimum of server side logic, a single server handler or MVC controller action if you wish, that is serving some HTML. And all the application logic is written in javascript. There are only AJAX calls to REST services from the client and all the view models live on the client. The server will only send an initial JSON model to the client but all the interaction with this model will happen on the client.

So while you can achieve lots of the logic in Razor, this stays server side processing. If you want a very dynamic application which is implemented entirely on the client side you might consider using such framework on the client.

like image 82
Darin Dimitrov Avatar answered Jan 01 '23 14:01

Darin Dimitrov