Home VS2017: WebSite (Razor3) JS and HTML Helpers not working in Dropdownlist
Reply: 0

VS2017: WebSite (Razor3) JS and HTML Helpers not working in Dropdownlist

user2350
1#
user2350 Published in August 15, 2018, 11:16 am

I would like to Display a town list based on a Province select name. I have to do the same thing with the following LINK in my web application but does not my website. I do;n't know what I am doing Wrong here.

  1. GetTown.cshtml page:

    @{ Layout = null; var Province_id = UrlData[0].IsEmpty() ? 1 : UrlData[0].AsInt(); var db = Database.Open("ConnectionString"); var sql = "SELECT TOP 13000 Town.Town_id, Town.City_Name, Province.Province_id FROM [dbo].[Town] JOIN [dbo].[Province] ON Town.Province_id = Province.Province_id WHERE p.Province_id =@0 ORDER BY City_Name ASC"; var towns = db.Query(sql, Province_id); Json.Write(towns, Response.Output); }

  2. JS SCript

    <script>
        $(function () {
            var productsSelect = $('#Town_id');
            productsSelect.attr('disabled', true);
            $('#Province_id').change(function () {
                var Province_id = $(this).val();
                $.getJSON('/GetTown/' + Province_id, function (towns) {
                    productsSelect.attr('disabled', false);
                    productsSelect.empty();
                    productsSelect.append(
                        $('<option/>')
                            .attr('value', '')
                            .text('-- Select Town --'));
                    $.each(towns, function (index, town) {
                        productsSelect.append(
                            $('<option/>')
                                .attr('value', town.Town_id)
                                .text(town.City_Name)
                        );
                    });
                });
            });
        });
    </script>
  1. Dropdown list for Province and Town
<select name="Province_id" id="Province_id" class="form-control">
   <option value="">-- Select Province --</option>
       @foreach (var grd in provinces)
       {
           <option value="@grd.Province_id" id="@oProvince">@grd.Province_Name</option>
       }
</select>
<select name="oTown" id="Town_id" class="form-control">
     <option value="">-- Select Town --</option>
</select>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.294305 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO