1

I have used an javascript for country,state,city for create client form.

     @Html.DropDownListFor(model => model.Country, new SelectList(new List<object>{
                                                                                                         new { value="Select", text= "--Select--" },                                                                                            
                                                                                                         }, "value", "text", 0), new { @class = "countries",id="countryId" })
 @Html.DropDownListFor(model => model.State, new SelectList(new List<object>{
                                                                                                     new { value="Select", text= "--Select--" },
                                                                                                     }, "value", "text", 0), new { @class = "states",id="stateId" })
@Html.DropDownListFor(model => model.City, new SelectList(new List<object>{
                                                                                                     new { value="Select", text= "--Select--" },
                                                                                                     }, "value", "text", 0), new { @class = "cities",id="cityId" })

For edit view, i simply loaded that country,state,city initially after that try to change onclick event

 @Html.DropDownListFor(model => model.Country, new SelectList(new List<object>{
                                                                                                     new { value=@ViewBag.country, text=@ViewBag.country },
                                                                                                     }, "value", "text", 0), new { @class = "countries", id = "countryId" })
  @Html.DropDownList("State", new SelectList(new List<object>{ new { value=@ViewBag.states, text=@ViewBag.states },
                                                                                                     }, "value", "text", 0), new { @class = "states", id = "stateId" })
  @Html.DropDownListFor(model => model.City, new SelectList(new List<object>{
                                                                                                     new { value=@ViewBag.city, text=@ViewBag.city },
                                                                                                     }, "value", "text", 0), new { @class = "cities", id = "cityId" })

in edit view i tried by following:

$("#countryId").click(function () {
                alert('test');
                @*<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script src="http://lab.iamrohit.in/js/location.js"></script>*@
            });

in jquery i call api in create page as following,

 @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://lab.iamrohit.in/js/location.js"></script>
    }

In edit page how can i call this api, so that i can reload country,state,city.. Can anyone help me to solve this.. Thanks in advance...

tereško
  • 58,060
  • 25
  • 98
  • 150
KaviSuja
  • 450
  • 1
  • 9
  • 37
  • but you didnt write code for catching onchange event , and no one is here to write the full code , so you better try some thing and ask a new question or update this question if you get any error. @KaviSuja. all the JS files you used are frameworks , you must write code to handle your requirement – J Santosh Sep 10 '15 at 09:29
  • i edited the question above. Please checked the above.... – KaviSuja Sep 10 '15 at 10:34
  • you want to load state and city in edit page or add page ? in edit it is simple , you pass country id to get states under that country and state id to get cities under that state and using jquery you have to do ajax call to get states and cities by passing ID – J Santosh Sep 10 '15 at 11:02
  • in add page i have loaded countries,states,cities. In edit page i want to show particulare coutnry,state,city to be selected from DB and when click it will show all other countries,cities,states – KaviSuja Sep 10 '15 at 11:32
  • i can't give you code, i can say logic 1. you have to bind country for change event and load states using ajax call and append them to States dropdown, similarly you have to bind states dropdown and do ajax call and append cities to cities dropdown. You shoud have methods in controller that will return states based on country id/name and cities based on state id/name. [Binding Dropddown](http://stackoverflow.com/questions/15089434/binding-onchange-to-dropdown), [Appending data to select](http://stackoverflow.com/questions/16160619/how-to-properly-append-json-result-to-a-select-option). – J Santosh Sep 10 '15 at 11:41

0 Answers0