简体   繁体   English

在MVC的部分视图中加载JQuery

[英]Loading JQuery in Partial View on MVC

I have an ASP.NET MVC C# web application. 我有一个ASP.NET MVC C#Web应用程序。

In my layout file I have the following ajax call 在我的布局文件中,我有以下ajax调用

  <li class="PriceModal">
                        @Ajax.ImageActionLink("/InternalDB/Content/downloadCSV.png", "Pareto", "35px", "35px", "PartialViewPriceCalculator",
                        "PriceCalculator", new {@Pareto = "active" }, new AjaxOptions
                        { UpdateTargetId = "PriceCalculator", InsertionMode = InsertionMode.Replace, HttpMethod = "GET"}, new { @style = "padding-top:30px" })
  </li>  

When this Ajax link is clicked a bootstrap Modal is loaded and a partial view is load inside the Bootstrap Modal. 单击此Ajax链接后,将加载引导模态,并在Bootstrap Modal内部加载局部视图。 At this point the Bootstrap modal and the partial view inside are sitting on top of my existing page. 此时,Bootstrap模态和内部局部视图位于我现有页面的顶部。

My problems start when I place jQuery code inside the partial View. 当我将jQuery代码放在部分视图中时,我的问题开始了。 I have found a way to append the jQuery code (partial) to the DOM with the following code 我发现了一种使用以下代码将jQuery代码(部分)添加到DOM的方法

 $('body').on('jQuery event to be placed here(focus,change,mouseenter)', 'class/ID to be Placed here', function (e) {       
    // jQuery events 
});

This will allow me to append to the DOM loaded by my page on the background. 这将允许我追加到我的页面在后台加载的DOM。 However this has a lot of issues as well. 但是,这也有很多问题。 How do I append a jQuery plug in called Chosen to a dropdownlist which is sitting inside my partial view. 我如何将名为Chosen的jQuery插件附加到位于我的局部视图内的dropdownlist中。

I have experimented by putting the jQuery inside the Ajax Options and using the Onsuccess property but this seems to have problems as well. 我通过将jQuery放在Ajax Options中并使用Onsuccess属性进行了实验,但这似乎也有问题。

 <li class="PriceModal">
                        @Ajax.ImageActionLink("/InternalDB/Content/downloadCSV.png", "Pareto", "35px", "35px", "PartialViewPriceCalculator",
                        "PriceCalculator", new {@Pareto = "active" }, new AjaxOptions
                        { UpdateTargetId = "PriceCalculator", InsertionMode = InsertionMode.Replace, HttpMethod = "GET", OnSuccess = "$('.chosen2').chosen({ allow_single_deselect : true })"}, new { @style = "padding-top:30px" })
  </li>  

Is there a general approach to loading all of the needed jQuery inside a popup partial view or i just need to find hacks for each specific situation? 有没有一种通用的方法可以将所有需要的jQuery加载到弹出式局部视图中,或者我只需要针对每种特定情况查找技巧?

You can just use the document.ready event to execute JavaScript when your PartialView finishes loading: 您可以在PartialView完成加载后使用document.ready事件执行JavaScript:

<div id="partialViewHere">
</div>

<script type="application/javascript">
    $(function() {
        // load whatever you need here
    };
</script>

I use this on an application to add events to buttons loaded through different PartialView s and it works perfectly. 我在应用程序上使用它来向通过不同PartialView加载的按钮添加事件,并且它可以完美地工作。

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

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