简体   繁体   中英

Passing data from partial view to parent view

I am trying to pass a value from partial view to parent view. I tried the below which didn't worked for me. Can some one help me out how can I achieve this? By using the value that partial view returned, I am trying to hide a button which is in the parent view.

Parent View:

    <div id="ProductCount">
    @{
        Html.RenderAction("ProductPartialView", "ProductList", new { area = "PR", ProductID = Model.ProductID, allowSelect = true});
    }

<div class="saveBtnArea">
    <input type="submit" value="Submit App" id="btn-submit" />
</div>

    jQuery(document).ready(function () {
            var ProductCount = jQuery("#ProductCount").val();

            if (ProductCount == 0) {
                jQuery("#btn-submit").hide();
            }
            else {
                jQuery("#btn-submit").show();
            }
        });

Partial View:

<fieldset>
<div>
<input type="hidden" id="ProductCount" value="5" />
</div>
</fieldset>

You can implement change event for hidden input field like this

$('#ProductCount').change(function(){
      var ProductCount = $(this).val();

      if (ProductCount == 0) {
            jQuery("#btn-submit").hide();
      }
      else {
            jQuery("#btn-submit").show();
      }
}).trigger('change');

$('#ProductCount').val(5);

 $('#ProductCount').change(function(){ var ProductCount = $(this).val(); if (ProductCount == 0) { jQuery("#btn-submit").hide(); } else { jQuery("#btn-submit").show(); } }).trigger('change'); $('#ProductCount').val(5); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type='hidden' id='ProductCount' /> <button id='btn-submit'>Submit</button> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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