简体   繁体   English

从ASP.NET MVC Partial View中调用JavaScript

[英]Call JavaScript from within an ASP.NET MVC Partial View

I have created an ASP.NET MVC partial view and I am calling it via the HTML.Action helper method: 我创建了一个ASP.NET MVC局部视图,我通过HTML.Action帮助器方法调用它:

@Html.Action("GetMyPartialView", "MyController", new { myParameter})

The partial view contains a control that needs some JavaScript to be called (a JavaScript library in an external JavaScript file). 局部视图包含一个需要调用JavaScript的控件(外部JavaScript文件中的JavaScript库)。 How can I call this JavaScript code from within my partial view. 如何在部分视图中调用此JavaScript代码。

I tried using the script element inside the partial view: 我尝试在局部视图中使用script元素:

<script>
    MyJavaScriptFunction();
</script>

This did not work. 这没用。 Probably the external JavaScript files (eg jQuery) have not been loaded at that time. 可能当时没有加载外部JavaScript文件(例如jQuery)。

What is the recommended way to execute JavaScript code when a partial view has been rendered? 在呈现局部视图时,建议使用JavaScript代码的方法是什么?

You cannot use java script sections in partial views. 您不能在部分视图中使用java脚本部分。 They simply don't work. 他们根本行不通。 So keep the @section JavaScript in the main view in order to register scripts and then render the partial view 因此,在主视图中保留@section JavaScript以便注册脚本然后渲染局部视图

I had almost similar situation. 我的情况几乎相似。 What i did was added javascript in the main view. 我做的是在主视图中添加了javascript。 You try add javascript in the main view from where you are calling 您尝试在主视图中添加javascript,从您调用的位置

 @Html.Action("GetMyPartialView", "MyController", new { myParameter})

You can use ajax call to achieve this. 您可以使用ajax调用来实现此目的。

$(document).ready(

    //Do ajax call  
        $.ajax({
        type: 'GET',
        url: "controller action url",    
        data : {                          
                  //Data need to pass as parameter                       
               },           
        dataType: 'html', //dataType - html
        success:function(result)
        {
           //Create a Div around the Partial View and fill the result
           $('#partialViewContainerDiv').html(result);                 
        }

//Action //行动

       public ActionResult GetMyPartialView(int myParameter)    
       {    
         //return partial view instead of View   
          return PartialView("YourView", resultSet);   
        }

I just ran into this problem. 我刚遇到这个问题。 You can call a javascript function in the partial view from within the partial view. 您可以在局部视图中从局部视图中调用javascript函数。 I created a hidden field and defined the onclick event to call the function I needed to call to initialize the dialog in the partial view. 我创建了一个隐藏字段并定义了onclick事件来调用我需要调用的函数来初始化局部视图中的对话框。 Then I triggered the click event for that hidden field. 然后我触发了该隐藏字段的click事件。

Code in partial view: 部分视图中的代码:

 <input type="hidden" id="initializeDialog" onclick="initializeDialog();" />

<script>
    function initializeDialog(){
        // Do stuff
    }
</script>

Code in containing view: 包含视图中的代码:

<script>
    $('#InitializeDialog').trigger('click');
</script>

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

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