Ros
Ros

Reputation: 634

ASP.NET MVC - Html.DropDownList

I would like to display a textbox depending on the value selected in the Html.DropDownList. Is this possible?

Upvotes: 0

Views: 3237

Answers (3)

JoshYates1980
JoshYates1980

Reputation: 3636

$('#SearchTypeID').change(function () {
    var searchValue = $('#SearchTypeID').val();

    if (searchValue == 4)
    {
        $('#DropdownNames').show();
        $('#TextboxNames').hide();
    }
    else
    {
        $('#TextboxNames').show();
        $('#DropdownNames').hide();
    }

})

Upvotes: 0

Fermin
Fermin

Reputation: 36111

Display where? Show/Hide a textbox depending on value in a drop down?

You could easily achieve this using the change event and jquery. Something like (untested)

$('#dropdownId').change(function(){
  var textbox = $('#textboxId');
  if ($(this).val() == 'foo')
    textbox.hide();
  else
    textbox.show();
});

Upvotes: 2

Razzie
Razzie

Reputation: 31232

You'll have to use javascript for that. Add an onchange event for the dropdownlist. Something like:

<%= Html.DropDownList("myList", myData, new { onchange = "showTextBox(this)" }) %>

And your myFunc will look along the lines of:

function showTextBox(item) {
  if(item.value == 'theCorrectValue')
  {
    document.getElementById('myTextBox').style.visibility = 'visible';
  }
}

If you use jQuery, it'll be slightly easier

Upvotes: 0

Related Questions