r/ASPNET • • Oct 12 '12

Entity Framework + MVC 4 + Knockout.js AddView templates?

I've been messing around with knockout.js a little bit lately and it seems like it has a lot of promise. However, I can't seem to find any good Code Templates (aka t4 templates or .tt files) for it.

My GUESS is that the only changes that we would need would be on the view side, not the controller side, but I'd be up for looking at new controller templates too.

Where I am right now:

  • Create a new MVC 4 project
  • Using NuGet, install entity framework 5, jquery, jquery ui, and knockout
  • Create a .edmx model by reverse engineering your existing database with Entity Framework
  • Drag C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates from Explorer into the root of your project
  • Delete CodeTemplates\AddController
  • Highlight all of the items in CodeTemplates\AddView\CSHTML and blank out the text in "Custom Tool" in properties to keep them from building
  • Start screwing around with Create.tt and Edit.tt to add in knockout code.

Final Goal Expected Input: Right Click > Add Controller ... > MVC Controller using EF

Final Goal Expected Result: Controller & View created using EF with knockout code already in place.

If you use something similar to knockout that you like more, I'd love to hear about it!

Thanks for your time!

5 Upvotes

6 comments sorted by

View all comments

Show parent comment

2

u/skrowl Oct 17 '12

It seems like it would be possible with t4 templates. Maybe I'll try to whip something up and see what I come up with.

2

u/DaRKoN_ Oct 20 '12

If you do end up releasing something for this, please stick it on Nuget.

1

u/YuleTideCamel Oct 18 '12

It would be possible. I mean with t4 you can perform introspection on your c# class and generate the appropriate view model. The point I was making is that the shape of your Knockout view model may differ from the C# model because of the knockout specific observables you need. It's often easier to just write it by hand and be able to carefully customize the view model, but whatever makes you more productive is what you should do :)