首页 > 解决方案 > Asp.NET core View 组件视图未触发 $(document).ready()

问题描述

我是 ASP.NET 核心的新手。我正在使用基于条件的视图组件在组件文件夹下加载不同的视图。

<div class="container" id="patientLayout">
    @await Component.InvokeAsync("PatientView", new {model = Model})
</div>

 public async Task<IViewComponentResult> InvokeAsync(PatientTabViewModel model)
        {
            switch (model.Id)
            {               
                case 3:
                    PatientMessageViewModel msgModel=GetMessagesViewModel(model.PatientId);
                    return await Task.FromResult(View("_messages.cshtml", msgModel));
             }
        }

我有一个$(document).ready()功能_Messages.cshtml

<script type="text/javascript">    
    $(document).ready(function () {
        console.log('ready function fired');        
        }
    });
</script>

问题:这个准备好的函数只是第一次被触发,但下一次它不会被触发,但断点每次都会在案例 3 处被击中。

您能否为此提供一些信息/解决方案?

标签: asp.net-core-mvcdocument-readyasp.net-core-viewcomponent

解决方案


仅当页面文档对象模型 (DOM) 准备好执行 JavaScript 代码时,其中包含的代码$( document ).ready()才会运行。$( window ).on( "load", function() { ... })一旦整个页面(图像或 iframe)(而不仅仅是 DOM)准备就绪,其中包含的代码就会运行。参考: $( document ).ready()


推荐阅读