Monday, 9 February 2015

How to clear/reset dropdownlist Using JQuery

Hi Folks,

While Working with Drodownlist's today i faced one problem it is not clearing the Old data.
Then realised to clear the data except default value using jQuery

My MVC Razor Code:

@Html.DropDownList("City", (IEnumerable<SelectListItem>)ViewBag.Cities, "--Choose Your City--", new { @id = "ddlCity", @style = "width:200px;" })

and Javascript section as below

@section Scripts {
    <script type="text/javascript">
function GetCities(_Id) {              
            var ddlcity = $("#ddlCity");   
                cache: false,
                type: "GET",
                url: '@Url.Action("GetCities", "Admin")',
                data: { "id": _Id },
                success: function (data) { 

   //Clearing the Data except first default value.

                    $.each(data, function (id, option) {
                error: function (xhr, ajaxOptions, thrownError) {
                    alert('Failed to retrieve Cities.');



No comments:

Post a Comment