较好地解决了在JavaScript code。使用Url.Action

编程入门 行业动态 更新时间:2024-10-26 00:22:32
本文介绍了较好地解决了在JavaScript code。使用Url.Action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在使用asp MVC 3(用剃刀)我当前的项目,当我做Ajax调用,我必须保持在JS视图页面上,因为我想用Url.Action生成的URL。这意味着我不能出去分裂JS code到.js文件,难道还有比我目前正在做的事情更好的解决方案。

In my current project using asp MVC 3 (using razor), when I'm making Ajax calls, I have to keep the JS on the view page because I want to use Url.Action to generate the URL. This means I can't split the js code out into .JS files, Is there a better solution than what I'm currently doing.

推荐答案

我倾向于,因为我喜欢我的命名空间中的JavaScript类似的方式如上做到这一点,一个稍微不同的扭曲。

I tend to accomplish this in a similar way as above, with a slightly different twist as I like namespacing my javascript.

JavaScript文件看起来是这样的:

The javascript file looks something like:

var my = {}; my.viewname = { init : function(settings){ // do some work here. settings.someImportantUrl has my json/ajax url eg. alert(settings.someImportantUrl ); } }

然后,我的观点将包含类似:

Then my view would contain something like:

<script language='javascript'> // allocate an object with variables you want to use in the external js file var settings = {someImportantUrl: '<%=Url.Action(...)%>'}; // call an init method for the current view my.viewname.init(settings); </script>

更多推荐

较好地解决了在JavaScript code。使用Url.Action

本文发布于:2023-11-03 20:41:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556042.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:较好   解决了   JavaScript   Action   Url

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!