ASP.NET Core MVC |身份插入设置为关闭(但我没有将其设置为关闭)

问题描述 投票:0回答:0

我有一个预订模型,应该存储有关客户的详细信息。该模型唯一值得注意的事情是,其中有一个属性存储从另一个视图/模型获取的 CarId

其工作原理如下:我单击“主页/索引”视图上的“查看详细信息”按钮,然后打开一个名为“详细信息”的部分视图,在该视图上,有一个“立即预订”按钮,可打开预订表格。此表格适用于预订模式。现在,“详细信息”视图应该传递选择到预订控制器中的汽车的 ID,但我想,要么我不知道如何做,要么就是非常困难。

请记住,我对 ASP.NET 非常陌生

这是我的预订模型

{
    public class Booking
    {
        [Key]
        public int Id { get; set; }

        public string FullName { get; set; }

        public string Country { get; set; }

        public string LicenseNumber { get; set; }

        public string LicenseIssueCountry { get; set; }

        public string PhoneNumber { get; set; }

        public string Email { get; set; }


        //Relationship Section For Car
        public int? CarId { get; set; }
        [ForeignKey("CarTypeId")]
        public virtual Car? Car { get; }


        [DisplayName("Pick Up Date")]
        public DateTime? PickUpDate { get; set; }

        [DisplayName("Drop Off Date")]
        public DateTime? DropOffDate { get; set; }

        public double RentalDuration{ get; set; }

        public int rentalTotal { get; set; }




        public double RentalDurationCalculator(DateTime dDate, DateTime pDate)
        {
            TimeSpan timeSpan = pDate - dDate;
            double rDuration = timeSpan.TotalDays;
            
            return rDuration;
        }


        public double TotalCalculator(int idGiven, double rDuration)
        {
            Car car = new Car();

            double rTotal = 0;

            if (car.Id == idGiven)
            {
                rTotal = car.CarPrice * rDuration;   
            }
            return rTotal;
        }
    }
}

这是我的预订控制器

using CarRentals.Models;
using CarRentals.Models.ViewModel;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace CarRentals.Controllers
{
    public class BookingController : Controller
    {
        private readonly ProjectDbContext _dbx;

        public BookingController(ProjectDbContext db)
        {
            _dbx = db;
        }

        public IActionResult Create()
        {
            Booking booking = new Booking();

            return View();
        }

        //POST for Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(Booking booking, int id)
        {
            if (ModelState.IsValid)
            {
                //_dbx.Database.ExecuteSqlRaw("SET IDENTITY_INSERT Bookings ON;");

                booking.RentalDuration = booking.RentalDurationCalculator((DateTime)booking.PickUpDate, (DateTime)booking.DropOffDate);
                booking.rentalTotal = (int)booking.TotalCalculator(id, booking.RentalDuration);

                _dbx.Bookings.Add(booking);
                _dbx.SaveChanges();

                //_dbx.Database.ExecuteSqlRaw("SET IDENTITY_INSERT Bookings OFF;");
            }

            return RedirectToAction("Index");
        }
    }
}

这是我的预订创建视图

@model CarRentals.Models.Booking
@{
    ViewData["Title"] = "Create";
}

<div class="row">
    <div class="container mt-5">
        <div class="row">
            <!-- Left Column - Image -->
            <div class="col-md-4">
                @*<src ="~/images/cars/@Model.Cars.CarImage1" alt="Card Image" class="img-fluid">*@
            </div>
            <!-- Right Column - Form -->
            <div class="col-md-8">
                <form asp-action="Create" method="post">
                    @*<input asp-for="@Model.Car.Id" hidden />*@
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                    <div class="form-group text-center">
                        <label asp-for="FullName">Full Name</label>
                        <input asp-for="FullName" type="text" class="form-control mt-2">
                    </div>
                    <div class="form-group text-center">
                        <label asp-for="Country">Country</label>
                        <input asp-for="Country" type="text" class="form-control mt-2">
                    </div>
                    <div class="form-group text-center">
                        <label asp-for="LicenseNumber">License Number</label>
                        <input asp-for="LicenseNumber" type="text" class="form-control mt-2">
                    </div>
                    <div class="form-group text-center">
                        <label asp-for="LicenseIssueCountry">License Issue Country</label>
                        <input asp-for="LicenseIssueCountry" type="text" class="form-control mt-2">
                    </div>
                    <div class="form-group text-center">
                        <label asp-for="PhoneNumber">Phone Number</label>
                        <input asp-for="PhoneNumber" type="text" class="form-control mt-2">
                    </div>
                    <div class="form-group text-center">
                        <label asp-for="Email">Email</label>
                        <input asp-for="Email" type="text" class="form-control mt-2">
                    </div>
                    <div class="form-group text-center" hidden>
                        <input asp-for="CarId" hidden /> @*value="@Model.Car.Id"*@
                    </div>
                    <div class="form-group text-center">
                        <label asp-for="PickUpDate">Pick-Up Date:</label>
                        <input asp-for="PickUpDate" type="date" class="form-control mt-2" placeholder="">
                    </div>
                    <div class="form-group text-center">
                        <label asp-for="DropOffDate">Drop-Off Date:</label>
                        <input asp-for="DropOffDate" type="date" class="form-control mt-2" placeholder="">
                    </div>
                    <div class="form-group mt-2 text-center">
                        <a asp-controller="CarList" asp-action="Index" class="btn btn-secondary">Back</a>
                        <input type="submit" value="Create" class="btn btn-success" @*asp-route-Id="@Model.Cars.Id"*@ />
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
c# asp.net entity-framework asp.net-core asp.net-core-mvc
© www.soinside.com 2019 - 2024. All rights reserved.