简体   繁体   English

在ASP.Net MVC视图中使用javascript和CSS文件?

[英]using javascript and css files in ASP.Net MVC View?

I'm having some trouble getting a jQuery plugin to work correctly in my MVC view. 我在使jQuery插件在MVC视图中正常工作时遇到了一些麻烦。 Here's what it looks like at the moment: (I've copied all needed jQuery includes and css files into the /Scripts folder of the site) 这是现在的样子:(我已将所有需要的jQuery include和css文件复制到站点的/ Scripts文件夹中)

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<script src="/Scripts/jquery-1.3.2.js" type="text/javascript"></script>
<script src="/Scripts/jquery.MetaData.js" type="text/javascript"></script>
<script src="/Scripts/jquery.rating.js" type="text/javascript"></script>
<link href="/Scripts/jquery.rating.css" rel="stylesheet" type="text/css" />

<input name="rating" type="radio" class="star" value="1"/>
<input name="rating" type="radio" class="star" value="2"/>
<input name="rating" type="radio" class="star" value="3"/>
<input name="rating" type="radio" class="star" value="4"/>
<input name="rating" type="radio" class="star" value="5"/>
</asp:Content>

The intellisense is squawking that the link element above can't be nested within a div tag (what div?) Nor can intellisense find the "star" class in any of the input tags. 智能感知者争辩说,上面的链接元素不能嵌套在div标签中(什么div?),智能感知者也无法在任何输入标签中找到“ star”类。

When I navigate to the page in question I don't see the stars, just a list of radio buttons. 当我导航到有问题的页面时,我看不到星星,只有单选按钮列表。 It's as if the jQuery stuff doesn't run. 好像jQuery的东西没有运行。

Where should I put the script and link tags in an MVC view? 我应该在MVC视图中的哪里放置脚本和链接标签? Anyone have small samples I can go by as an example? 有人可以举一些小样本为例吗?

I'm going over this example from "Professional ASP.Net MVC" from Wrox and the Ajax chapter leaves something to be desired, as the code is all snippets with no complete pages. 我正在阅读Wrox的“ Professional ASP.Net MVC”示例,而Ajax章节有一些不足之处,因为代码都是没有完整页面的代码段。 The Wrox website chapter code for this book also consists of the same snippets, one per text file, and is of no use. 本书的Wrox网站章节代码也包含相同的摘要,每个文本文件一个,并且没有用。

Thanks! 谢谢!

In your master page file, create a content placeholder in the head area. 在母版页文件的顶部区域中创建内容占位符。 You can use this whenever you need to insert scripts and styles into specific views, meaning you won't deviate from the guidelines stating that this is where such files should go. 每当需要在特定视图中插入脚本和样式时,都可以使用此方法,这意味着您不会偏离说明这些文件应放在哪里的准则。

In your Site.Master file: 在您的Site.Master文件中:

<head>
<!-- head stuff here -->
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>

In your view aspx file: 在您的视图aspx文件中:

<asp:Content ContentPlaceHolderID="HeadContent" runat="server">
    <script src="/Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.MetaData.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.rating.js" type="text/javascript"></script>
    <link href="/Scripts/jquery.rating.css" rel="stylesheet" type="text/css" />
</asp:ContentPlaceHolder>

<asp:Content ContentPlaceHolderID="MainContent" runat="server">
    <input name="rating" type="radio" class="star" value="1"/>
    <input name="rating" type="radio" class="star" value="2"/>
    <input name="rating" type="radio" class="star" value="3"/>
    <input name="rating" type="radio" class="star" value="4"/>
    <input name="rating" type="radio" class="star" value="5"/>
</asp:Content>

You will need to put the <link /> in the <head>...</head> section the script tags should be fine. 您需要将<link />放在<head>...</head>部分中,脚本标记应该没问题。 You can put it in the head of a master page. 您可以将其放在母版页的开头。

It's also a useful excercise to use a Helper method to get the correct location of your Script and css files. 使用Helper方法来获取Script和CSS文件的正确位置也是一种有用的练习。

<script src="<%= Html.Content("~/Scripts/jquery-1.3.2.js") %>
        type="text/javascript"></script>

please check in fiddler and see wat kind of response ur getting when u call the particular file ....if the script is rendered properly and for that particular page ...if no then please post the response and a copy .. 请检入提琴手 ,看看当您调用特定文件时得到的响应类型....如果脚本正确渲染且针对该特定页面...如果否,则发布响应和副本。

i got a similar problem when i was working with js in mvc i wasn't able to load the j query ...so for that i created a helper class which returns the absolute path for me 当我在MVC中使用js时遇到类似的问题,但我无法加载j查询...因此,我创建了一个帮助程序类,该类为我返回了绝对路径

    public static string ResolveUrl(this HtmlHelper html, string virtualUrl)
    {
        Control con = new Control();
        var RelativeUrl = con.ResolveUrl(virtualUrl);
        if (!virtualUrl.StartsWith("~/"))
            return RelativeUrl;

        virtualUrl = virtualUrl.Remove(0, 2);

        string applicationPath = html.ViewContext.HttpContext.Request.ApplicationPath;
        if (string.IsNullOrEmpty(applicationPath) || !applicationPath.EndsWith("/"))
        {
            applicationPath = applicationPath + "/";
        }

        return applicationPath + virtualUrl;
      }
  } 

and in my view i called the page like this ... 在我看来,我这样称呼页面...

<script src="<%= Html.ResolveUrl("~") %>Scripts/jquery-1.3.2.js" type="text/javascript"></script>

Are you using a master page for your site? 您是否正在使用网站的母版页? If so you could put all your include tags in the master page, it would then be available to all views using the master page. 如果是这样,您可以将所有包含标记放在母版页中,然后使用母版页的所有视图都可以使用它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM