MVC中JQuery文件引入的路径问题,@Url.Content函数
2014-03-11 19:06
309 查看
今天写了个MVC的Demo,文件夹结构很简单,如下:
利用EF生成Model框架并手工加表字段注解,但在页面上JS验证始终没显示。实在无语。
无意中在浏览器里按F12,看见提示:
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:1445/~/Scripts/jquery.validate.min.js
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:1445/~/Scripts/jquery.validate.unobtrusive.min.js%22
估计是路径JS文件的路径问题,赶紧看了下Layout.cshtml视图:
果然,11,12,13行的代码运行时会被映射到:
映射后的物理路径有问题。实际Jquery在网站根目录下,而不再Test文件夹下!
把出错的这几行去掉,用@Url.Content()函数映射,将虚拟(相对)路径转换为应用程序绝对路径。
PS:要想使用MVC的JS自动验证,除了在Model写注解、引入jquery-1.5.1.min.js外,还要注意是否引入了jquery.validate.js。从名字即可得知,那是JS的验证方法集。
默认的MVC3空项目和Internet的_Layout.cshtml不引用jquery以及validate.js.
利用EF生成Model框架并手工加表字段注解,但在页面上JS验证始终没显示。实在无语。
无意中在浏览器里按F12,看见提示:
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:1445/~/Scripts/jquery.validate.min.js
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:1445/~/Scripts/jquery.validate.unobtrusive.min.js%22
估计是路径JS文件的路径问题,赶紧看了下Layout.cshtml视图:
<head> <meta charset="utf-8" /> <title>@ViewBag.Title</title> <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @*映射错误的路径*@ <script type="text/javascript" src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> <script type="text/javascript" src="~/Scripts/jquery.validate.min.js"></script> <script type="text/javascript" src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> </head>
果然,11,12,13行的代码运行时会被映射到:
映射后的物理路径有问题。实际Jquery在网站根目录下,而不再Test文件夹下!
把出错的这几行去掉,用@Url.Content()函数映射,将虚拟(相对)路径转换为应用程序绝对路径。
PS:要想使用MVC的JS自动验证,除了在Model写注解、引入jquery-1.5.1.min.js外,还要注意是否引入了jquery.validate.js。从名字即可得知,那是JS的验证方法集。
默认的MVC3空项目和Internet的_Layout.cshtml不引用jquery以及validate.js.
相关文章推荐
- jQuery源码学习之总体架构
- jQuery EasyUI 1.3 中文教程
- 如何判断一个对象是否为jquery对象
- 如何判断一个对象是否为jquery对象
- 如何判断一个对象是否为jquery对象
- 如何判断一个对象是否为jquery对象
- jquery 选择器大全的详细说明和实例
- jquery image slider plugin
- jquery JSON的解析方式
- 探索高效jQuery的奥秘
- 利用jquery和flash实现复制文字功能(等同于ctrl+c)
- 模拟jQuery中的ready方法及实现按需加载css,js实例代码
- jquery实现全选、反选
- jQuery数组处理全解
- jQuery获取Select选择
- 关于项目通过jQuery-cometd的使用...
- jquery选择器、事件
- jqplot formatString 日期格式化列表
- visual studio 2010的jquery智能提示
- jquery 回车事件