c# - Asp.Net MVC Model with related drop downs -
i building screen in c# asp.net mvc application, has few related drop downs.so, example, select 'category', , 2nd drop down should display related sub categories selected category.
so, model contain list, used build category drop down, , list, empty. on selection of category, need post back, passing id if selected category, populate list<> sub categories... , build sub category drop down?
hopefully can assist design of model. model have quite few non-list related data item being edited. , think model need 'categoryselectedid', know selected?
the nicest way use ajax this. you'll have hook change
event first select box, , once changed, you'll ajax request selected value action
. action return json list, parsed , put in next select box
.
update
the model json returning action can anonymous type really, or ienumerable
of selectlistitem
. use linq
: `mycollection.select(item=> new selectlistitem() { name = item.name, value = item.id.tostring() });
if assume page looks this:
<form> <select id="maincat" name="maincatid"> <option value="1">first option</option> <option value="2">first option</option> <option value="3">first option</option> </select> <select id="subcat" name="subcatid" disabled="disabled"> <option value=""> -- please select main category -- </option> </select> </form>
your model like:
public class mymodel { public int maincatid {get;set;} public int subcatid {get;set;} public ienumerable<selectlistitem> maincats {get;set;} }
of course add validation attributes etc.
Comments
Post a Comment