0

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>
2
  • What is it that is supposed to be returned? And what do you want to do. Commented Jul 4, 2019 at 1:38
  • Also you have 2 elements with id of ProductCount: div and input. Thus when you select jQuery("#ProductCount") it will select both. This is probably the issue. Commented Jul 4, 2019 at 2:32

1 Answer 1

0

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.