Dropdown List from JsonResult
In your controller pass back data from a JsonResult method
public JsonResult allactiveprograms()
{
var programs = programProvider.getActivePrograms();
List mylist = new List();
foreach (var p in programs)
{
program d = new program();
d.programID = p.programID;
d.programName = p.programName;
mylist.Add(d);
}
return Json(new SelectList(mylist.ToArray(), "programid", "programname"), JsonRequestBehavior.AllowGet);
}
On your page - your script will look something like this:
$(document).ready(function(){
if ($("#SearchType :selected").text() == "Program") {
$('#Programs').html("");
$('#ddl').css('display', '');
$.getJSON($('#rootpath').val() + '/piaresults/allactiveprograms', function (data) {
var items = '';
$.each(data, function (i, programs) {
items += "";
});
//This next line adds our items into our placeholder
$('#Programs').html(items);
$('input#SearchTerm').hide();
$('#SearchLabel').hide();
$('.btn').hide();
});
}
});
Your Container to hold the list
@Html.DropDownList("Programs", new SelectList(string.Empty, "Value", "Text"), "-- Select Program --", htmlAttributes: new { @class = "form-control required" })
public JsonResult allactiveprograms()
{
var programs = programProvider.getActivePrograms();
List
foreach (var p in programs)
{
program d = new program();
d.programID = p.programID;
d.programName = p.programName;
mylist.Add(d);
}
return Json(new SelectList(mylist.ToArray(), "programid", "programname"), JsonRequestBehavior.AllowGet);
}
On your page - your script will look something like this:
$(document).ready(function(){
if ($("#SearchType :selected").text() == "Program") {
$('#Programs').html("");
$('#ddl').css('display', '');
$.getJSON($('#rootpath').val() + '/piaresults/allactiveprograms', function (data) {
var items = '';
$.each(data, function (i, programs) {
items += "";
});
//This next line adds our items into our placeholder
$('#Programs').html(items);
$('input#SearchTerm').hide();
$('#SearchLabel').hide();
$('.btn').hide();
});
}
});
Your Container to hold the list
@Html.DropDownList("Programs", new SelectList(string.Empty, "Value", "Text"), "-- Select Program --", htmlAttributes: new { @class = "form-control required" })
Comments