I have a dropdown for tradertype which on selection should populate the dropdown for traders. My view looks like :
`<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 a Trader type from here<strong></strong></span></li>
<li>
<label>
<span class="mandatory">*</span>Trader:</label>
<select name="Trader" id="Trader">
</select>
<span class="tagline">Select a Trader from here<strong></strong></span></li>
</ul>`
I tried using JQuery, but I couldn't get the change event of the 'TraderType' dropdown. My 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) {
for (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 the 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 help me on this.