jquery - Cascading dropdown using asp.net mvc -
i have dropdown tradertype on selection should populate dropdown traders. view looks :
`<ul> <li> <label> <span class="mandatory">*</span>trader type:</label> <%=html.dropdownlist("tradertype", (selectlist)viewdata["tradertype"])%> <%--<select id="ddltradertype" name="tradertype"> <%foreach (selectlistitem item in (selectlist)viewdata["tradertype"]) { %> <option value="<%=item.value %>"> <%=item.text %></option> <%} %> </select>--%> <span class="tagline">select trader type here<strong></strong></span></li> <li> <label> <span class="mandatory">*</span>trader:</label> <select name="trader" id="trader"> </select> <span class="tagline">select trader here<strong></strong></span></li> </ul>`
i tried using jquery, couldn't change event of 'tradertype' dropdown. script is:
$("tradertype").change(function() { alert("change"); $.ajax({ url: $("#listtraders").attr("action"), type: 'get', contenttype: "application/json; charset=utf-8", cache: false, data: { part: $("#tradertype").val() }, datatype: 'json', async: false, success: function(data) { if ((data.lsttraders.length) > 0) { (var count = 0; count < data.lsttraders.length; count++) { $("#trader").append("<option value='" + data.lsttraders[count].id.tostring() + "'>" + data.lsttraders[count].tradername + "</option>"); } } } }); });
the code in controller is:
public jsonresult listtraders(string trdrtypeid) { ilist<hstrader> lsttraders = new list<hstrader>(); build objbld = new build(); document objdoc = new document(); string message = string.empty; bool result = true; try { int trdrtype = convert.toint32(trdrtypeid); lsttraders = objbld.gettradersbytrdrtypeid(trdrtype); } catch (exception ex) { message = ex.message; result = false; } return json(new { success = result, message = message, lsttraders = lsttraders });}
please me on this.
$("#tradertype").change(function() { });
you're missing # action in controller try , change verb in post
$.ajax({ url: $("#listtraders").attr("action"), type: 'post',
change controller this:
[httppost] public jsonresult listtraders(string trdrtypeid)
... , when return json should change this:
return json(new { success = result, message = message, lsttraders = lsttraders }, jsonrequestbehavior.denyget);
another thing, parameters must match
data: { trdrtypeid: $("#tradertype").val() }, public jsonresult listtraders(string trdrtypeid)
Comments
Post a Comment