添加到脚本包从局部视图中.NET MVC 5 [英] Adding to script bundle from partial view in .NET MVC 5

查看:139
本文介绍了添加到脚本包从局部视图中.NET MVC 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在被将要使用呈现为HTML.Partials部件()。净MVC websolution。我们希望能够为局部视图中添加其dependencys在主要页面浏览量脚本标签,无论是CSS-文件以及JavaScript文件。

我一直想在我这部分code。但是JS文件的心不是我的页面布局部分呈现。什么是错的?

  @ {    VAR包= System.Web.Optimization.BundleTable.Bundles.GetRegisteredBundles()
        。凡(B = GT; b.Path ==〜/包/ jQuery的)
        。第一();    bundle.Include(〜/脚本/ addtojquerybundletest.js);
}


解决方案

它只是似乎是具体到我的jQuery包。现在的作品,我添加了一个widgetspecific包,您可以添加脚本主要布局来呈现。束还处理相同的文件多次加法和唯一renderes一次(如果同一插件存在几次在同一页上,可以发生)。我的解决方案如下补充说,这似乎不被那么好从香港专业教育学院迄今所做的搜索记录。

在BundleConfig.cs:

  bundles.Add(新ScriptBundle(〜/ widgetspecific));

在_Layout.cshmtl:

  @ Scripts.Render(〜/ widgetspecific)

在Widget.cshtml:

  @ {
    变种束= System.Web.Optimization.BundleTable.Bundles.GetBundleFor(〜/ widgetspecific);    bundle.Include(〜/脚本/ andreas.js);
}

是任何人都知道的消极方面,该解决方案?

We are making a .Net MVC websolution that is going to be using widgets rendered as HTML.Partials(). We would like to be able for the partial view to add its dependencys in the main page views scripts tags, both css-files as well as javascript files.

Ive been trying with this code in my partial. But the js-file isnt rendered in the layout section of my page. What is wrong?

@{

    var bundle = System.Web.Optimization.BundleTable.Bundles.GetRegisteredBundles()
        .Where(b => b.Path == "~/bundles/jquery")
        .First();

    bundle.Include("~/Scripts/addtojquerybundletest.js");
}

解决方案

It just seemed to be specific to my jquery bundle. It now works, I have added a widgetspecific bundle where you can add scripts to render in the main layout. The bundle also handles the adding of the same file several times (which can happen if the same widget exists several times on the same page) and only renderes it once. My solution added below, which doesnt seem to be that well documented from the searches Ive done so far.

In BundleConfig.cs:

bundles.Add(new ScriptBundle("~/widgetspecific"));

In _Layout.cshmtl:

@Scripts.Render("~/widgetspecific")

In Widget.cshtml:

@{
    var bundle = System.Web.Optimization.BundleTable.Bundles.GetBundleFor("~/widgetspecific");

    bundle.Include("~/Scripts/andreas.js");
}

Is anyone aware of negative aspects to this solution?

这篇关于添加到脚本包从局部视图中.NET MVC 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆