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 asp.net MVC Razor Code:
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 asp.net 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");
$.ajax({
cache: false,
type: "GET",
url: '@Url.Action("GetCities", "Admin")',
data: { "id": _Id },
success: function (data) {
//Clearing
the Data except first default value.
ddlcity.children('option:not(:first)').remove();
$.each(data, function (id, option) {
ddlcity.append($('<option></option>').val(option.id).html(option.name));
});
},
error: function (xhr, ajaxOptions,
thrownError) {
alert('Failed to retrieve Cities.');
}
});
}
</script>
}
No comments:
Post a Comment