asp.net mvc - Fill a combo depending on user choice -
i have combo 1 contains quantity , combo 2 contains max value selected in combo 1 how can set values of combo 2 after user change combo 1
combo 1:
@html.dropdownlistfor(model => model.qtitetobuy, new selectlist( new list<object>{ new { value = 0 , text = "0"}, new { value = 1 , text = "1"}, new { value = 2 , text = "2"}, new { value = 3 , text = "3"}, new { value = 4 , text = "4"}, new { value = 5 , text = "5"}, new { value = 6 , text = "6"}, new { value = 7 , text = "7"}, new { value = 8 , text = "8"}, new { value = 9 , text = "9"} }, "value", "text"))
if user choose combo1 = 4 should have in combo 2 0 4
@melom made sample here should started. let me know if have further questions.
to summarize first populated combo1 on postback followed populating combo2 using ajax.
view:
<div> @html.dropdownlist("combo1", (ienumerable<selectlistitem>)viewbag.combo) <select id="combo2"></select> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $('#combo1').change(function(){ $.ajax({ type: "get", url: "@url.action("getsecondcombodata", "home")", data: { val : $(this).val() }, success: function (data) { var options = $("#combo2"); options.empty(); $.each(data, function() { options.append($("<option />").val(this).text(this)); }); }, datatype: 'json' }); }); }); </script>
action:
[httpget] public actionresult index() { var values = new list<dynamic> { new { value = 0 , text = "0"}, new { value = 1 , text = "1"}, new { value = 2 , text = "2"}, new { value = 3 , text = "3"}, new { value = 4 , text = "4"}, new { value = 5 , text = "5"}, new { value = 6 , text = "6"}, new { value = 7 , text = "7"}, new { value = 8 , text = "8"}, new { value = 9 , text = "9"} }; viewbag.combo = new selectlist(values, "value", "text"); return view(); } [httpget] public jsonresult getsecondcombodata(int val) { return json(enumerable.range(0, val+1), jsonrequestbehavior.allowget); }
Comments
Post a Comment