Click here to Skip to main content
15,915,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
public ActionResult Index()
        {
            var environments = new SelectList(new[]
            {
                new { ID = 0, Name = string.Empty },
                new { ID = 1, Name = "Jake" },
                new { ID = 2, Name = "Tom" },
                new { ID = 3, Name = "Dan" },
                new { ID = 4, Name = "Stu" },
                new { ID = 5, Name = "Pete" }
            }, "ID", "Name");

            ViewData["Environments"] = environments;

            return View();
        }


cshtml code

<div align="center">@Html.DropDownList("Environments", ViewData["Environments"] as        SelectList)
</div>

<div align="center"></div>@{Html.RenderAction("ShowGrid");}

<section id="scripts">
<script type="text/javascript">
    $('#Environments').change(function () {
        ApplicationErrors.Refresh();
    });
</script>
</section>`

Js code

function OnBeginCallback(s, e) {
e.customArgs["environment"] = $('#Environments').val();
}`                
Posted
Comments
Richard C Bishop 6-Feb-14 16:09pm    
You should use the "SelectedIndexChaged" event of the dropdownlist. It will fire every time a new selection is made.
Member 10577367 7-Feb-14 15:11pm    
Where would I insert the event in my code example? Sorry I'm a novice still
Richard C Bishop 7-Feb-14 15:13pm    
The easiest way is to double-click the dropdownlist and it will do the work for you. The event handler signature will show up in your dropdownlist tag and the event handler will be created in the code behind.
Member 10577367 7-Feb-14 15:22pm    
Sorry, maybe I do not fully understand. When I double click all I get it a message box stating that the code returns a single item. I am using VS to create the view

<div>@Html.DropDownList("Environments", ViewData["Environments"] as SelectList ) </div>
Member 10577367 7-Feb-14 15:22pm    
What is the other way? Maybe I can try that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900