Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

How to show only those items in drop-down list that have relation with another drop-down list?

$
0
0

I have 2 options from FlightId drop-down list and when i click on SeatId i have items from both 2 options(flight 815 and 416) but i want to get only items for the selected flight.

So if i select 815 i want to get the seats only from a1 to a4, if you select 416 you should get only seats for those flight b1 to b4.

but in my case i`m getting every seat.

So here`s my models:

public class Plane
    {
        [Key]
        public int PlaneId { get; set; }
        public string PlaneModel { get; set; }

        public virtual ICollection<Seat> Seats { get; set; }
        public virtual ICollection<Flight> Flights { get; set; }        
    }
public class Flight
    {
        [Key]
        public int FlightId { get; set; }
        public string FlightCode { get; set; }                      

        public int PlaneId { get; set; }
        public virtual Plane Plane { get; set; }                   
    }
public class Seat
    {
        [Key]
        public int SeatId { get; set; }
        public string SeatNumber { get; set; }
        public bool isSeatTaken { get; set; }

        public int PlaneId { get; set; }
        public virtual Plane Plane { get; set; }
    }


and my controller:

public ActionResult Create()
        {
            ViewBag.FlightId = new SelectList(db.Flights, "FlightId", "FlightCode");
            ViewBag.SeatId = new SelectList(db.Seats, "SeatId", "SeatNumber");            
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "BookingId,PassengerName,FlightId,UserId,SeatId")] Booking booking)
        {
            if (ModelState.IsValid)
            {
                booking.UserId = User.Identity.GetUserId();
                db.Bookings.Add(booking);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.FlightId = new SelectList(db.Flights, "FlightId", "FlightCode", booking.FlightId);
            ViewBag.SeatId = new SelectList(db.Seats, "SeatId", "SeatNumber", booking.SeatId);            
            return View(booking);
        }

view:

@model vAirline.Core.Entities.Booking

@{
    ViewBag.Title = "Create";
}<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal"><h4>Booking</h4><hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })<div class="form-group">
            @Html.LabelFor(model => model.PassengerName, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10">
                @Html.EditorFor(model => model.PassengerName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PassengerName, "", new { @class = "text-danger" })</div></div><div class="form-group">
            @Html.LabelFor(model => model.FlightId, "FlightId", htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10">
                @Html.DropDownList("FlightId", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.FlightId, "", new { @class = "text-danger" })</div></div><div class="form-group">
            @Html.LabelFor(model => model.SeatId, "SeatId", htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10">
                @Html.DropDownList("SeatId", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.SeatId, "", new { @class = "text-danger" })</div></div><div class="form-group"><div class="col-md-offset-2 col-md-10"><input type="submit" value="Create" class="btn btn-default" /></div></div></div>
}


Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>