简体   繁体   English

Javascript无法在PartialView上运行

[英]Javascript does not run on PartialView

I am using MVC4, with C# and Razor to make a simple Web Application. 我正在将MVC4与C#和Razor一起使用,以制作一个简单的Web应用程序。

In my Controller I have a method that returns a partial view that is shown to the user, in this case, a modal popup: 在我的控制器中,我有一个方法可以返回显示给用户的局部视图,在这种情况下为模式弹出窗口:

public ActionResult GetPackageDetails(int id)
{ 
    return PartialView("_EditPackageModal", new ModalEditPackage());
} 

The partial view, which is a modal popup shows as predicted, but does not run any javascript: 局部视图是一个模态弹出窗口,如预期的那样显示,但未运行任何javascript:

@model SuperApp.Model.ModalEditPackage

<div class="modal fade bs-example-modal-lg" id="editPackageModal" tabindex="-1" role="dialog" aria-labelledby="editPackages" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="myModalLabel">Edit Package @Model.packageName</h4>
            </div>
            <div class="modal-body">

                <div>
                    <label class="control-label">Package Name: </label>
                    <input type="text" class="form-control" id="package-name" placeholder="@Model.packageName">
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

This is the javascript code that I wish to run on the partial view: 这是我希望在部分视图上运行的javascript代码:

<script>
    $("input").click(function () {
        alert("hello world!");
    });
</script>

After reading this question: 看完这个问题:

I realized that I must include the script in the partial view. 我意识到我必须在局部视图中包含脚本。 To sum up: 总结一下:

  1. The partial view is unable to run the JS code that is in my project. 部分视图无法运行我项目中的JS代码。
  2. The partial view only runs JS code that is directly specified in the partial view using the <script> tag 局部视图仅运行使用<script>标记在局部视图中直接指定的JS代码。

Now, in this example such is fine, but in the real world I have several scripts that work on the partial view, and they are considerably big. 现在,在此示例中这很好,但是在现实世界中,我有几个脚本可用于部分视图,而且它们很大。 Furthermore, the client already downloaded them because they are being used in other views and I don't know how to tell the partial view where the scripts are located since I am using Bundles. 此外,客户端已经下载了它们,因为它们正在其他视图中使用,并且由于我正在使用捆绑软件,所以我不知道如何告诉局部视图脚本的位置。

Is there a way to fix this without smashing the script directly into the partial view? 有没有一种方法可以解决此问题,而无需将脚本直接砸到部分视图中?

give unique name to all your modals or html blocks. 为所有模态或html块赋予唯一的名称。 Make your js code like this: 使您的js代码如下所示:

$(document).on('click', '#yourSpecifiedDomElement input','click',function(){
    alert("hello world!");
});

place all your javascript code to specified js files which included in bundles. 将您所有的JavaScript代码放入捆绑包中包含的指定js文件中。

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

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