我可以使用服务器端Javascript来编写Razor中的视图吗?

时间:2022-03-09 16:56:47

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.

我最近与一位没有c#经验的前端开发人员合作,他将为ASP编写视图。NET MVC3应用程序,我正在构建。

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.

由于大多数web开发人员都知道Javascript,我认为让他在Javascript而不是c#中进行服务器端代码剪切是非常棒的。我知道除了输出变量等之外,视图中没有太多代码,但是有一些基本的循环等等。

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

我想做的事情可能吗?这是一个节点。js的事情吗?(我根本没看过。)

5 个解决方案

#1


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.

ASP。NET MVC要求您使用。NET 4.0。因此,您可以使用编译到MSIL的任何CLS兼容的语言来构建服务器端。就javascript而言,JScript。NET现在已经过时了,目前我还没有其他的选择。

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.

就Razor而言,解析器支持的语言只有c#和VB.NET。如果您想使用其他语言,您必须为它构建一个解析器,或者使用另一个视图引擎。

#2


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

您可能对此感兴趣:一个javascript视图引擎。使用某种JS模板引擎并不需要太多的工作

https://github.com/mcintyre321/JsViewEngine

https://github.com/mcintyre321/JsViewEngine

#3


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.

我怀疑一个优秀的Javascript程序员在获取c# /VB代码时是否会遇到很多麻烦。Net需要写一个视图。

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.

然而,一个可能的JS唯一的替代方法是只让你的视图输出JS,也许是一个基本的HTML布局,然后使用ajax调用控制器动作来获取数据,并只使用JS动态构建视图的HTML。在我看来,这似乎是一种过于复杂的方法,但如果UI开发人员不能掌握一些简单的c#编程技巧,那么这可能是一种解决方案。

#4


0  

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

我使用过:https://github.com/pauldotknopf/JavaScriptViewEngine,它仍然在更新。

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.

通过这种方式,您可以为Javascript视图(服务器端js——js视图引擎)设置Razor视图(服务器端c# - Razor视图引擎)的替代方案。您还可以将它用于React和/或Javascript。

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

我使用它,所以服务器端代码+前端代码是相同的;

#5


-1  

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

您可以在Razor页面上使用javascript和html。你甚至可以把这两者结合起来。

<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?

但是如果你只打算这么做的话,剃刀可能就有点过头了。Razor非常擅长做简单的循环,不应该是很难学的。您是否已经为您的朋友构建了视图模型?

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

当然,一些javascript开发将完全绕过视图模型。相反,使用ajax调用来检索模型数据,并在客户端维护其状态(例如,主干鼓励这种方法)。

#1


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.

ASP。NET MVC要求您使用。NET 4.0。因此,您可以使用编译到MSIL的任何CLS兼容的语言来构建服务器端。就javascript而言,JScript。NET现在已经过时了,目前我还没有其他的选择。

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.

就Razor而言,解析器支持的语言只有c#和VB.NET。如果您想使用其他语言,您必须为它构建一个解析器,或者使用另一个视图引擎。

#2


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

您可能对此感兴趣:一个javascript视图引擎。使用某种JS模板引擎并不需要太多的工作

https://github.com/mcintyre321/JsViewEngine

https://github.com/mcintyre321/JsViewEngine

#3


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.

我怀疑一个优秀的Javascript程序员在获取c# /VB代码时是否会遇到很多麻烦。Net需要写一个视图。

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.

然而,一个可能的JS唯一的替代方法是只让你的视图输出JS,也许是一个基本的HTML布局,然后使用ajax调用控制器动作来获取数据,并只使用JS动态构建视图的HTML。在我看来,这似乎是一种过于复杂的方法,但如果UI开发人员不能掌握一些简单的c#编程技巧,那么这可能是一种解决方案。

#4


0  

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

我使用过:https://github.com/pauldotknopf/JavaScriptViewEngine,它仍然在更新。

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.

通过这种方式,您可以为Javascript视图(服务器端js——js视图引擎)设置Razor视图(服务器端c# - Razor视图引擎)的替代方案。您还可以将它用于React和/或Javascript。

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

我使用它,所以服务器端代码+前端代码是相同的;

#5


-1  

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

您可以在Razor页面上使用javascript和html。你甚至可以把这两者结合起来。

<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?

但是如果你只打算这么做的话,剃刀可能就有点过头了。Razor非常擅长做简单的循环,不应该是很难学的。您是否已经为您的朋友构建了视图模型?

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

当然,一些javascript开发将完全绕过视图模型。相反,使用ajax调用来检索模型数据,并在客户端维护其状态(例如,主干鼓励这种方法)。