Garrett Daniel DeMeyer
Garrett Daniel DeMeyer

Reputation: 931

Using MVC, how can I show or hide elements based on a checkbox?

I am trying to learn how to use MVC, and something I want to be able to do is have the user check a box to show or uncheck to hide any number of elements.
I have seen this done where the whole page does not refresh and the user does not need to click any "submit" button, but it does it live.
All the tutorials for MVC I found in my search do not seem to answer this question, or even give me a proper idea of partial views (which I think might be the solution). I understand this question is silly, but I have been looking for a couple hours and can not figure it out.

Upvotes: 2

Views: 7215

Answers (3)

Bassam Mehanni
Bassam Mehanni

Reputation: 14944

To do this kind of action without a page refresh you need to use javascript or jQuery

let's say your view looks like this:

<input type="checkbox" id="myCheckbox">
<div id="ShowHideMe">
   <p>some content</p>
</div>

you need something like,

<script>
  $(function() {
    $('#myCheckbox').change(function() {
       $('#ShowHideMe').toggle($(this).is(':checked'));
     });
  });
</script>

You use javascript, whenever you wanna do client-side programming.

Upvotes: 4

SunilK
SunilK

Reputation: 157

Use jquery.

$("#myCheckbox").click(function () { $("#someOtherElementId").hide(); });

Upvotes: 0

Forty-Two
Forty-Two

Reputation: 7605

I have seen this done where the whole page does not refresh and the user does not need to click any "submit" button, but it does it live

You are describing javascript, not any built in functionality of (presumably, asp.net)mvc

Upvotes: 0

Related Questions