Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi.I am new here.I know little angular,asp.net mvc ,jquery,json but cannot use them and combine in one project.So I have dropdown list-contry,trans Name,vagon Name.I want choose trainsName,after that find in database count Vagon of this choosen Train.And show dropdown list this vagon.

As I understand I need to pass from View to Controller which train is choosen,then find from database CountVagon(for example Train-T0000000023 has 5 vagon) And pass from Controller to View DropdownList(which consist-Vagon1,Vagon2,Vagon3,Vagon4,Vagon5).

Please explain me,if somebody can solve that.I have searched many time but couldnt do exatly what I wanted.Thank you :))

What I have tried:

<script type="text/javascript">
    $(document).ready(function () {
        $('#Name').change(function () {
         var TrainID= $('#Name :selected').val();

        });

    });
    
</script>

<h2>Buy</h2>

<div class="table table-bordered">

 
    
    <div class="filter">

        @Html.DropDownListFor(x => x.City, (SelectList)ViewBag.TrainsCity)

        @Html.DropDownListFor(x => x.Name, (SelectList)ViewBag.TrainsName)

        @Html.DropDownListFor(x => x.VagonCount, (List<SelectListItem>)ViewBag.TrainsVagon)

    </div>
       



ANd Controller

  public ActionResult Buy()
        {
            ViewBag.TrainsName = new SelectList(db.Trains, "ID", "Name");
            ViewBag.TrainsCity = new SelectList(db.Trains, "ID", "City");

            //which train or where choosed angular will receive and get the number of train by using lambda expression and pass to the methods
            //I need to pass countVagon instead of 7
            ViewBag.TrainsVagon = PopulateCountVaqon(7);
            PopulateCountVaqon(7);
            IEnumerable<TrainReqistration.Models.Train> trains = db.Trains;
            return View();
        }

        List<SelectListItem>  PopulateCountVaqon(int count)
        {
            List<SelectListItem> countVagon = new List<SelectListItem>();
            for(int i = 0; i <= count; i++)
            {
                countVagon.Add(new SelectListItem() { Value = i.ToString(), Text = "Vaqon " + i });
            }

            return countVagon;
        }
Posted
Updated 23-Sep-18 8:39am
v2

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