5

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.)

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Razor has support for non-C# languages? I want to code the views, not controllers, using js.
@DavidPfeffer, as far as the views are concerned Razor supports only C# and VB.NET. The parser has built-in support for those 2 languages only. I think you should look for an alternative view engine. But honestly, if you code your server side using C#, it would be very strange not to use C# if you ever decide to use Razor as a view engine.
The goal was to make it comfortable for the view developer, who doesn't know C#. Employing people with js and HTML experience is a lot easier than C# and HTML experience.
@DavidPfeffer, you can't do this with Razor.
1

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

Comments

0

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.

Comments

0

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

Comments

-1

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).

5 Comments

This doesn't even remotely answer the question of whether js can be used as a server side language for Razor pages.
Razor is not a language, it is a view engine.
Razor is a view engine: you are correct. The questioner mentioned the developer wanted to do the server-side code snips in javascript inside of a Razor view. This code is an example (on an actual project) of how we've done that. You have to know a little Razor still, but you are able to combine the two (to a degree). I think that's worth pointing out.
The "questioner" is me. What you've done is client-side js using the server-side model (sent down as JSON), which isn't at all what was asked. Note that the question clearly says how I want to use server-side js rather than C#.
Aha. I suppose I wasn't sure what you meant by combining server-side and client-side js in a single view. Darin Dimitrov's answer is more to the point on that issue. My answer is some code that follows K. Bob's suggestion. We had a developer who was more comfortable in javascript than C# and this is the approach we used.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.