简体   繁体   English

ASP.NET MVC-加载局部视图时的Ajax微调器

[英]ASP.NET MVC - Ajax Spinner while loading a Partial view

I'm using ASP.NET MVC and I'm trying to use an Ajax Spinner. 我正在使用ASP.NET MVC,并且正在尝试使用Ajax Spinner。 When the user submit a definied button, an ajax spinner should be displayed while my Partial View is builed and displayed. 当用户提交定义的按钮时,应在构建和显示我的局部视图的同时显示ajax微调器。 Here is what I'm doing. 这是我在做什么。

Here's my button and the jQuery code linked to this button. 这是我的按钮以及与此按钮链接的jQuery代码。 The suggestionsContainer represents the container where the Partial View will be rendered : proposalContainer表示将呈现部分视图的容器:

<input type="button" value="Suggérer rdv" id="SuggestBtn" disabled ="disabled"/>


<div id="suggestionsContainer">

</div>

    $('#SuggestBtn').click(function() {

        var intervals = scheduler.GetSelectedInterval();
        //var $hiddenInput = jQuery('input:hidden', jQuery(this));
        //$hiddenInput.val(intervals.ToString());

        var chk = $('#chbxPADC').is(':checked');
        var chk1 = $('#chbxPADP').is(':checked');
        var chk2 = $('#chbxPremPADC').is(':checked');
        var chk3 = $('#chbxPremPADP').is(':checked');
        var chk4 = $('#chbxOther').is(':checked');
        var chkMan = $('#Homme').is(':checked');
        var chkWoman = $('#Femme').is(':checked');
        var NbPADC = $('#NbPADC').val();
        var NbPADP = $('#NbPADP').val();

        var isFemale = true;
        if (chkMan == true)
            isFemale = false;


        PADP = chk1;
        PADC = chk;
        PremedicationPADC = chk2;
        PremedicationPADP = chk3;
        autre = chk4;



        $.get('/Home/GetSuggestions', { PADP: PADP, PADC: PADC, isFemale : isFemale, PremedicationPADP: PremedicationPADP, PremedicationPADC: PremedicationPADC, NbPADC: NbPADC, NbPADP: NbPADP, autre: autre, intervals: intervals.ToString() }).success(function (html) {
            $('#suggestionsContainer').html(html);
        });
    });

I've read this StackOverflow Q&A but as you can see I'm not using the @Ajax.ActionLink. 我已经阅读了此StackOverflow问答,但是正如您所看到的,我没有使用@ Ajax.ActionLink。

Any idea of what to do here? 有什么想法在这里做什么?

Assuming all your controls are in a form with an ID of myForm You could do something like 假设所有控件都在一个ID为myForm的表单中,则可以执行以下操作

$('#SuggestBtn').click(function() {
   //maybe check if form is valid first... eg if ($('#myForm').validate().form()){...
   //start client side spin/progress bar
   $.ajax(
     type: "POST",
     url: "/Controller/Method",
     data: $('#myForm').serialize(),
     success: function (response){
        $('#suggestionsContainer').html(response);
        //end client side spin/progress bar
     }
   )
}

We are using the jQuery ajax function to request the partial view. 我们正在使用jQuery ajax函数来请求部分视图。 (the url parameter should be set to your action method that returns the partial view) The form data that is posted to your partial view is gotten from $('#myForm').serialize() . (应将url参数设置为返回部分视图的操作方法)从$('#myForm').serialize()发布到部分视图的表单数据。 You should note that no unobstrusive validation will be performed client side here unless you explicitly do so here. 您应该注意,除非您在此处明确执行此操作,否则不会在客户端执行无干扰的验证。

Once it comes back from the server the success function gets invoked (assuming its a status 200 http response) which in this example replaces the contents of the suggestionsContainer with the returned HTML. 从服务器返回的success函数将被调用(假定其状态为200 http响应),在本示例中,该函数用返回的HTML替换proposalContainer的内容。

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

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