使用asp.net mvc的级联下拉列表 [英] Cascading dropdown using asp.net mvc
本文介绍了使用asp.net mvc的级联下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有tradertype一个下拉其中的选择应填充贸易商的下拉。
我的看法是这样的:
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>`
我尝试使用JQuery,但我无法得到'TraderType下拉列表中的更改事件。
我的脚本是:
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>");
}
}
}
});
});
在控制器中的code是:
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 });}
请帮我在这。
推荐答案
我建议你在这里使用的控件;
I recommend you use the controls here;
演示: http://demo.aspnetawesome.com/AjaxDropdownDemo
阿贾克斯下拉控件可以很容易地用于级联的下拉列表中。
The Ajax Dropdown control can easily be used for cascading dropdowns.
另外还有很多其他有用的控制这里。
Plus there are lots of other useful controls here as well.
这篇关于使用asp.net mvc的级联下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文