asp.net mvc - In MVC3 how do I have a dropdown that shows/hides fields based on the selected value? -
i have dropdown of countries, , address form. depending on country selected, want hide/show fields. i'm quite new mvc , mvc3, best way this?
i have on page 'dropdownlistfor' populates correctly. when changes, imagine need ask server fields show/hide. perhaps put jquery change event calls method, , returns json saying visible:true each field, don't know if that's ideal or how implement (possibly $.ajax or something).
any ideas?
edit: should add hard part of asking server fields show each country there many countries , possibilities stored in database. accustomed webforms not mvc ordinarily postback , have serverside logic, isn't option mvc afaik...
i have deleted first answer irrelevant.
with mvc3 can send ajax request method.
in homecontroller.cs
:
public list<string> getfieldstoshow(string id) { // if routing left default, parameter passed in called 'id' // gotta do... list<string> listoffieldstoshowbasedoncountry = getlist(id); return listoffieldstoshowbasedoncountry; }
and in ajax call, like...
$.ajax({ type: 'post', url: '/home/getfieldstoshow/' + valueofselecteddropdownitem, /*etc...*/ success: function(data){ $(data).each(function(){ $('#' + this).show(); } } });
Comments
Post a Comment