从 ASP.NET MVC 部分视图中调用 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30478252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call JavaScript from within an ASP.NET MVC Partial View
提问by Alexander
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). How can I call this JavaScript code from within my partial view.
分部视图包含需要调用一些 JavaScript 的控件(外部 JavaScript 文件中的 JavaScript 库)。如何从我的局部视图中调用此 JavaScript 代码。
I tried using the script element inside the partial view:
我尝试在局部视图中使用脚本元素:
<script>
MyJavaScriptFunction();
</script>
This did not work. Probably the external JavaScript files (e.g. 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 代码的方法是什么?
采纳答案by Spider man
I had almost similar situation. What i did was added javascript in the main view. You try add javascript in the main view from where you are calling
我有几乎类似的情况。我所做的是在主视图中添加了 javascript。您尝试在主视图中从您调用的位置添加 javascript
@Html.Action("GetMyPartialView", "MyController", new { myParameter})
回答by Sachu
You cannot use java script sections in partial views. 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
您不能在部分视图中使用 java 脚本部分。它们根本不起作用。所以将@section JavaScript 保留在主视图中,以便注册脚本然后渲染局部视图
回答by Gera
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);
}
回答by Jim Berg
I just ran into this problem. You can call a javascript function in the partial view from within the partial view. 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. Then I triggered the click event for that hidden field.
我刚刚遇到了这个问题。您可以在局部视图中调用局部视图中的 javascript 函数。我创建了一个隐藏字段并定义了 onclick 事件来调用我需要调用的函数来初始化局部视图中的对话框。然后我触发了该隐藏字段的点击事件。
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>