Hi Devson,
I do this sort of thing by creating a SelectList in a controller and passing it to the view as part of a view model. Then in the view, I have the option of doing something when the SelectedValue changes, or simply returning the value of the dropdown when something else triggers a call to a controller.
Controller code:
int count = 0;
List<string> YearList = new List<string>();
for (int i = 2000; i < (DateTime.Now.Year + 6); i += 4)
{
YearList.Add(i.ToString());
if (i < iyear)
count++;
}
var q = from c in doc.Descendants("candidate")
select new can_sw_model
{
name = c.Attribute("name").Value,
office = c.Parent.Attribute("name").Value.ToUpper(),
party = c.Attribute("party").Value,
};
can_sw_view model = new can_sw_view()
{
YearList = new SelectList(YearList),
value = YearList[count],
model = q,
};
return View(model);
View code:
<script type="text/javascript">
$(document).ready(function() {
$('#YearList').val('<%= Model.value %>');
$('#YearList').change(function(event) {
window.location.replace('<%= ResolveUrl("~/Candidate/sw_candidates") %>' + "?year=" + $('#YearList').val());
});
});
function pdfclick() {
var grid = $("#grid1").data("tGrid");
window.location.replace('<%= ResolveUrl("~/Candidate/pdf") %>' + "?year=" + $('#YearList').val() + "&tab=statewide" +
"&page=" + grid.currentPage + "&orderBy=" + grid.orderBy + "&groupBy=" + grid.groupBy + "&filterBy=" + grid.filterBy);
}
</script>
I hope this helps!
Bob