Can I use server-side Javascript to code views in Razor?

I've recently partnered up with a front-end developer with no C# experience, who is going to be coding the views for an ASP.NET MVC3 application I am building.

Since most web developers know Javascript, I thought it would be awesome to have him do the server-side code snips in Javascript rather than C# within Razor views. I know there isn't much code in views beyond outputting variables, etc., but there is some basic looping, etc.

I also really like the idea of having both client-side and server-side code in the views be the same language.

Is what I want to do possible? Is this a Node.js thing? (I haven't looked at that at all.)

Answers


ASP.NET MVC requires you to use .NET 4.0. So you can use any CLS compliant language that compiles to MSIL to build the server side. As far as javascript is concerned, well, JScript.NET is now obsolete and currently I am not aware of an alternative.

As far as Razor is concerned, the only languages that the parser supports are C# and VB.NET. If you want to use some other language you will have to either build a parser for it or use an alternative view engine.


You might be interested in this: a javascript view engine. It wouldn't take much work to make it work with some kind of JS templating engine

https://github.com/mcintyre321/JsViewEngine


I doubt that a decent Javascript programmer would have much trouble picking up the little bits of C#/VB.Net needed to write a view.

However, one possible JS only alternative would be to have your view output JS only and maybe a basic HTML layout and then use ajax to call controller actions to get data and build the HTML for the view on the fly using only JS. To me that would seem like an overly complicated way to do it but if your UI dev can't get the hang of a few simple C# programming skills then that might be a solution.


I have used : https://github.com/pauldotknopf/JavaScriptViewEngine and it's still updated.

This way, you setup an alternative for Razor Views ( server side c# -- Razor ViewEngine) for Javascript Views ( server side js -- JS ViewEngine). You can also use it for React and/or Javascript.

I used it so server-side code + frontend code was the same ;)


You can use javascript and html on a Razor page. You can even combine the two: e.g.

<script>
     $(document).ready(function() {
       var note = {
          workflow: @Html.Raw(Json.Encode(Model))
       };
       // do something with note
    });
</script>

But Razor is probably overkill if that's all you plan to do. Razor is more than capable of doing simple loops and shouldn't be a hardship to learn. Are you already building out the view model for your friend?

Of course, some javascript development will bypass view models altogether. Instead, ajax calls are used to retrieve model data and its state is maintained on the client side (e.g., backbone encourages this approach).


Need Your Help

copy date from one excel workbook to another excel workbook with pushing macro button

excel excel-vba macros vba

I want to copy data from one excel workbook to another excel workbook(master work book) with pushing macro button.