Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How to bind values from Database in DropDownList using Razor in MVC3 ??
Posted

1 solution

In the Script tag :


XML
<script type="text/javascript">
    $(document).ready(function () {



        $("#CountryID").change(function () {
//            alert("Hi");
            var url = '@Url.Content("~/")' + "UserInfo/BindCity";
            var ddlSource = "#CountryID";
            var ddlTarget = "#Cities";

            $.getJSON(url, { c: $(ddlSource).val() }, function (data) {
                $(ddlTarget).empty();
                $.each(data, function (index, optionData) {
                    $(ddlTarget).append("<option value='" + optionData.Value + "'>" + optionData.Text + "</option>");
                });
            });
        });
    });

</script>




In the Razor View


   <td>
                    <div class="editor-label">
                        @Html.Label("Country")
                    </div>
                    <div class="editor-field">
                        @Html.DropDownList("CountryID", (IEnumerable<SelectListItem>)ViewData["Ct"],
new { id = "CountryID" })
                        <br />
                        @* @Html.ValidationMessageFor(model => model.ContactNo)*@
                    </div>
                    <div class="editor-label">
                        @Html.Label("City")
                    </div>
                    <div class="editor-field">
                        @Html.DropDownList("Cities")
                    </div>
                </td>





Its Work Fine For me....
try this out..
Best Of Luck....
 
Share this answer
 
Comments
Kuthuparakkal 20-Sep-12 7:18am    
my 5+
[no name] 20-Sep-12 9:02am    
thanks for that bro ...

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