简体   繁体   English

我可以使用服务器端Javascript在Razor中对视图进行编码吗?

[英]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. 我最近与一个没有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. 由于大多数Web开发人员都知道Javascript,所以我认为让他在Javascript中而不是在Razor视图中使用C#进行服务器端代码剪裁真是太棒了。 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? 这是Node.js吗? (I haven't looked at that at all.) (我根本没有看过。)

ASP.NET MVC requires you to use .NET 4.0. ASP.NET MVC要求您使用.NET 4.0。 So you can use any CLS compliant language that compiles to MSIL to build the server side. 因此,您可以使用编译为MSIL的任何CLS兼容语言来构建服务器端。 As far as javascript is concerned, well, JScript.NET is now obsolete and currently I am not aware of an alternative. 就javascript而言, JScript.NET现在已经过时了,目前我还没有其他选择。

As far as Razor is concerned, the only languages that the parser supports are C# and VB.NET. 就Razor而言,解析器仅支持C#和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. 您可能对此感兴趣:javascript视图引擎。 It wouldn't take much work to make it work with some kind of JS templating engine 使它与某种JS模板引擎一起工作不需要太多工作

https://github.com/mcintyre321/JsViewEngine https://github.com/mcintyre321/JsViewEngine

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). 这样,您就可以为Razor视图(服务器端c#-Razor ViewEngine)和Javascript视图(服务器端js-JS ViewEngine)设置替代方案。 You can also use it for React and/or Javascript. 您也可以将其用于React和/或Javascript。

I used it so server-side code + frontend code was the same ;) 我使用了它,因此服务器端代码+前端代码是相同的;)

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. 但是,一种可能的仅使用JS的替代方法是仅使视图输出JS,并且可能具有基本的HTML布局,然后使用ajax调用控制器动作来获取数据并仅使用JS即时为视图构建HTML。 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. 在我看来,这似乎是一种过于复杂的方法,但是如果您的UI开发人员无法掌握一些简单的C#编程技能,那么这可能是一个解决方案。

You can use javascript and html on a Razor page. 您可以在Razor页面上使用javascript和html。 You can even combine the two: eg 您甚至可以将两者结合起来:

<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可能会大材小用。 Razor is more than capable of doing simple loops and shouldn't be a hardship to learn. Razor不仅能够执行简单的循环,而且学习起来也不难。 Are you already building out the view model for your friend? 您已经为您的朋友建立视图模型了吗?

Of course, some javascript development will bypass view models altogether. 当然,一些javascript开发将完全绕过视图模型。 Instead, ajax calls are used to retrieve model data and its state is maintained on the client side (eg, backbone encourages this approach). 取而代之的是,使用ajax调用来检索模型数据,并在客户端维护其状态(例如,骨干网鼓励这种方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以将javascript与javascript中的服务器端代码混合并匹配吗? - Can I mix & match javascript with server-side code in javascript? 如何在服务器端使用JavaScript代码? - How to use JavaScript code on server-side? 我可以在服务器端标记内使用JavaScript变量吗? - Can I use a JavaScript variable within Server-Side Tags? 我可以在服务器端使用某些客户端Javascript API吗? - Can I use certain client-side Javascript APIs server-side? 为什么我要使用服务器端JavaScript? - Why would I want to use server-side JavaScript? 如何在服务器端Blazor中使用Bing Javascript映射? - How can I use a Bing Javascript Map in server-side Blazor? 我可以通过ASP.NET将客户端javascript中的对象发送到服务器端代码吗? - Can I send an object from client-side javascript to server-side code via ASP.NET? GWT:如何使用服务器端排列选择? - GWT: How can I use Server-Side Permutation Selection? 如何在razor视图中的服务器端代码之间编写JavaScript? - How can i write JavaScript between server side code in razor view? 如何将服务器端ASP XmlHttpRequest代码转换为客户端JavaScript? - How do I convert my server-side ASP XmlHttpRequest code to client-side JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM