|
[View]
<input type="text" id="dropdownslist" />
<div id="carsList">
<ul>
//Grouping1###
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</div>
<input type="text" id="DropDown" />
</div>
</div>
<br/><br/>
<script type="text/javascript">
$(function () {
$("#dropdownslist").ejDropDownList({
targetID:"carsList",
change: "onChange",
width:"150px"
});
$("#DropDown").ejDropDownList({
dataSource: @Html.Raw(Json.Encode(ViewData["DataSrc"])),
fields: { text: "Text", value: "Value" },
value: "item1",
text: "List Item 1",
});
});
function onChange(args) {
var obj = $('#dropdownslist').data("ejDropDownList");
var id = obj.getSelectedValue();
//ajax request for postback
var data = JSON.stringify({ Pid: id });// send value
$.ajax({
type: "POST",
url: "/Dropdownlist/GetWeeksInYear",
async: false,
dataType: "json",
data: data,
contentType: "application/json",
success: function (data) {
debugger
var obj = $("#DropDown").data("ejDropDownList");
obj.setModel({ //append to the data to dropdownlist
dataSource: data
});
},
error: function () {
alert('Error Loading Data');
},
});
}
</script>
[controller]
[HttpPost]
public ActionResult GetWeeksInYear(string Pid)
{
List<DropDownValue> newData = new List<DropDownValue>();
var items = Data();
foreach (var item in items)
{
if (item.Pid == Pid)
newData.Add(item);
}
return Json(newData); //postback the data
} |