asp.net mvc - if i select dropdown option 1 then i get another dropdown only one record -
i have 2 dropdown , 1st static 'displayanswer' , 2nd 'correctanswer' here select 'displayanswer' option 2 1 , 2 in 'correctanswer' 2nd dropdown, or select 3 1, 2 , 3 in 'correctanswer' 2nd dropdown, or select 4 1, 2, 3 , 4 in 'correctanswer' 2nd dropdown. using mvc razor.
plz me.
thnks.
this can achieved using jquery
irrespective if using asp.net mvc
. 1 way of achieving need , there might other solutions can follow, worked me.
below samples code, can modify fit in scenario. in sample going working departments , units when adding new user. department consists out of different units. when department selected first drop down department's units displayed in second drop down.
let me start view (page). accepts view model called createuserviewmodel
. view model should used instead of passing through domain model.
@model myproject.viewmodels.users.createuserviewmodel
the html markup representing 2 drop downs:
<tr> <td class="edit-label">department <span class="required">*</span></td> <td> @html.dropdownlistfor( x => x.departmentid, new selectlist(model.departments, "id", "name", model.departmentid), "-- select --", new { id = "departments" } ) @html.validationmessagefor(x => x.departmentid) </td> </tr> <tr> <td class="edit-label">unit <span class="required">*</span></td> <td> @html.dropdownlistfor( x => x.unitid, new selectlist(model.units, "id", "name", model.unitid), "-- select --", new { id = "units" } ) @html.validationmessagefor(x => x.unitid) </td> </tr>
my createuserviewmodel
class (partial):
public class createuserviewmodel { public int departmentid { get; set; } public ienumerable<department> departments { get; set; } public int unitid { get; set; } public ienumerable<unit> units { get; set; } }
my department
class:
public class department { public int id { get; set; } public string name { get; set; } }
my unit
class:
public class unit { public int id { get; set; } public string name { get; set; } public int departmentid { get; set; } public virtual department department { get; set; } }
when view loaded first time populate departments drop down , have units drop down empty.
public actionresult create() { createuserviewmodel viewmodel = new createuserviewmodel { departments = departmentservice.getall(), units = enumerable.empty<unit>() }; return view(viewmodel); }
getting view (page) used jquery
populate second drop down:
<script> function resetdropdown(targetdropdown) { var items = ''; items = '<option>-- select --</option>'; $(targetdropdown).empty(); $(targetdropdown).html(items); } $(document).ready(function () { $("#departments").change(function () { var url = '@url.action("getunitsbydepartmentid", "user")'; var departmentid = $("#departments").val(); if (departmentid == '') { resetdropdown($('#units')); return; } $.getjson(url, { departmentid: departmentid }, function (data) { $('#units').empty(); var items = '<option>-- select --</option>'; $.each(data, function (i, unit) { items += "<option value='" + unit.id + "'>" + unit.name + "</option>"; }); $('#units').html(items); }); }); }); </script>
in controller there action method returns units json
result:
public actionresult getunitsbydepartmentid(int departmentid) { ienumerable<unit> units = unitservice.getunitsbydepartmentid(departmentid); var displayedunits = units.select(x => new { id = x.id.tostring(), name = x.name }); return json(displayedunits, jsonrequestbehavior.allowget); }
this solution might not perfect guide in right direction. best.
Comments
Post a Comment