I have this code on controller side to initialise the drop down
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem
{Text = "All",
Value = "All"
});
items.Add(new SelectListItem
{
Text = "val1",
Value = "val1"
});
items.Add(new SelectListItem
{
Text = "val2",
Value = "val2"
});
ViewData["DDLItems"] = items;
// I have added this code because I am passing the selection back to this action
if (Activefilter == null){ Activefilter = "All" ;}
ViewData["Activefilter"] = Activefilter;
On the view, I have this code
<%= Html.DropDownList("ActiveFilter", (IEnumerable<SelectListItem>)ViewData["DDLItems"], new { onChange = "func(1)" })%>
for jquery , I have this
$(document).ready(function() {
$('#Activefilter').val(<%= ViewData["Activefilter"].ToString() %>);
}
so whenever , i change the value of the dropdown, the action is called as shown above. the viewdata["Actionfilter"] also takes the value selected by the user but when the control comes back to the view,
$('#Filter').val(<%= ViewData["Activefilter"].ToString() %>);
in this line , it says the value "All" is undefined.
Everytime, the page reloads, I want the dropdown to retain the selected value. can you please help ?