简体   繁体   English

停止在加载时调用 Html.Action

[英]Stop Html.Action from being called on Load

I have below div inside a parent view and the below Html.Action returns a PartialView我在父视图中有下面的div ,下面的Html.Action返回一个PartialView

<div id="preview" class="modal fade" role="dialog">
<div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
        @Html.Action("GetPrintView", "Connector")
    </div>
</div>

I open the above modal using below jquery:我使用下面的 jquery 打开上面的modal

$("#printdoc").click(function(e) {
    e.preventDefault();
    $(this).attr('data-target', '#preview');
    $(this).attr('data-toggle', 'modal');
});

The issue is, whenever the parent .cshtml is loaded it calls the above Html.Action , but this should be only called when clicked on button.问题是,每当加载父.cshtml时,它都会调用上面的Html.Action ,但这应该只在单击按钮时调用。 I tried using data-url for the div but still the action is being called repeatedly.我尝试对div使用data-url ,但仍然重复调用该操作。 Any help?有什么帮助吗?

Html.Action will execute the action when view is loaded. Html.Action将在加载视图时执行操作。 Since you don't want this, You need load data asynchronously.既然你不想要这个,你需要异步加载数据。

You can store URL in custom data-* attribute to generate the URL use Url.Action .您可以将 URL 存储在自定义data-*属性中以使用Url.Action生成 URL。 On click of the button load the partial view using .load()单击按钮时,使用.load()加载局部视图

HTML HTML

<div class="modal-content" data-url='@Url.Action("GetPrintView", "Connector")'>        
</div>

Script脚本

$("#printdoc").click(function (e) {
    e.preventDefault();

    var modal = $("#preview .modal-content"); //Find the element
    $(modal).load(modal.data('url')); //Fetch url and load partial view

    $(this).attr('data-target', '#preview');
    $(this).attr('data-toggle', 'modal');
});

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

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