2

I want to hide some data when I select a value in my dropdownList.

Example :

When I choose Gender = M, I don't want to see in my Title : Mr. Only Miss or Madame.

Here is my code :

     @Html.DropDownListFor(m => m.Gender, new[] {
                           new SelectListItem() {Text = "M", Value = "M"},
                           new SelectListItem() {Text = "F", Value = "F"},
                           }, "---Choose Gender---", new { onchange = "Select();" })

     @Html.DropDownListFor(m => m.Title, new[] {
                           new SelectListItem() {Text = "Mister", Value = "Mr."},
                           new SelectListItem() {Text = "Madame", Value = "Mme."},
                           new SelectListItem() {Text = "Miss", Value = "Miss."}
                            }, "---Choose Title---")

In Javascript Section :

   function Select() {
        // the code.
    }
2
  • You should be more specific. Where is the Title that you want to modify? Is it the title of the Html page? Commented Aug 29, 2013 at 12:03
  • You should also show what you've tried so far. What you've posted so far looks like you couldn't be bothered & have asked SO to solve the problem for you. Commented Aug 29, 2013 at 12:17

1 Answer 1

5

You can do this with the help of JQuery,

  function changeGender()
    {
    if($('#Gender').val()=='M')
    {
    $('#Title').html('');
    $('#Title').html('<option value="Mister">Mr.</option>');
    }
    else if($('#Gender').val()=='F')
    {
    $('#Title').html('');
    $('#Title').html('<option value="Madame">Madam</option><option value="Miss">Miss.</option>');
    }
    else
    {
    $('#Title').html('');
    }
return false;
    }

Hope this helps.

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.