根据另一个字段填充一个字段

时间:2018-07-05 16:49:37

标签: asp.net asp.net-mvc

继续使用我的车辆预订系统,我已经开始从事储备功能本身的工作,我一直在研究如何根据车场填充驾驶员场。有两种类型的车辆:办公室类型的车辆具有以前功能的依赖驱动程序,然后供应商类型的车辆最初没有驱动程序,直到您可以从下拉字段中进行选择的预订功能为止。在我的控制器代码中,供应商类型与DriverID链接为0。到目前为止,我不确定如何实现if语句。

控制器:

 //RESERVE
    public ActionResult ReserveInitial()
    {

       ViewBag.DriverID = new SelectList(db.Drivers, "DriverID", "DriverLastName");
       ViewBag.VehicleID = new SelectList(db.Vehicles, "VehicleID", "VehicleMake");

        var checkVehicle = db.ReservedVehicleDetails.Count(x => x.VehicleID == 1);




        return View();
    }

    [HttpPost]
    public ActionResult ReserveInitial(ReservationViewModel RV)
    {
        var repo = new ManagementRepository();
        var resini = new ReservedVehicleInitial
        {
            Id = RV.Id,
            BorrowerLastName = RV.BorrowerLastName,
            BorrowerFirstName = RV.BorrowerFirstName,
            VehicleID = RV.VehicleID,
            Reason = RV.Reason,
            ProjectID = RV.ProjectID,
            Location = RV.Location,
            DateReserved = RV.DateReserved,
            TimeStart = RV.TimeStart,
            DateCreated = DateTime.Now,
            UserID = RV.UserID,
            DriverID = RV.DriverID

        };

                    Driver driver = new Driver();
                    if (resini.DriverID == 0)
                    {
                        ViewBag.DriverID = new SelectList(db.Drivers, "DriverID", "DriverLastName" + ", " + "DriverFirstName");

                    }

                    else
                    {
                        using (var db = new VehicleReservationEntities())
                        {
                            ViewBag.DriverID = driver.DriverID;
                        }
                    }

        using (var db = new VehicleReservationEntities())
        { 
            repo.ReserveInitial(resini);
        }
        return RedirectToAction("List");
    }

查看模型:

    public class ReservationViewModel
{
    public int Id { get; set; }
    public string BorrowerLastName { get; set; }
    public string BorrowerFirstName { get; set; }
    public DateTime DateReserved { get; set; }
    public int? VehicleID { get; set; }
    public int? DriverID { get; set; }
    public string VehicleMake { get; set; }
    public  string DriverName { get; set; }
    public string VehicleType { get; set; }
    public string OdometerStart { get; set; }
    public string OdometerEnd { get; set; }
    public string Liters { get; set; }
    public string AmountPaid { get; set; }
    public string ORNumber { get; set; }
    public string Location { get; set; }
    public string Reason { get; set; }
    public int? ProjectID { get; set; }
    public DateTime DateCreated { get; set; }
    public string UserID { get; set; }
    public TimeSpan TimeStart { get; set; }
    public TimeSpan TimeEnd { get; set; }
    //
    public string BorrowerFullName
    {
        get { return BorrowerLastName + ", " + BorrowerFirstName; }
        set { }
    }
    //
    public string DriverLastName { get; set; }
    public string DriverFirstName { get; set; }
    public string FullName { get; set; }
}

查看:

     <div class="col-md-12">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="form-group control-label">
                                        <label>
                                            Vehicle
                                        </label>
                                    </div>
                                    <div id="Vehicle">
                                        @Html.DropDownList("VehicleID", null, "", htmlAttributes: new { @class = "form-control", @id = "VehicleID" })
                                    </div>
                                </div>
                            </div>


                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="form-group control-label">
                                        <label>
                                            Driver
                                        </label>
                                    </div>

                                <div id="Driver">
                                    @Html.DropDownList("DriverID", null, "", htmlAttributes: new { @class = "form-control", @id = "DriverID" })                                        @Html.ValidationMessageFor(model => model.DriverID, "", new { @class = "text-danger" })
                                    @Html.ValidationMessageFor(model => model.DriverID, "", new { @class = "text-danger" })
                                </div>
                            </div>
                            </div>

0 个答案:

没有答案