• 1401/07/18

ارور Object reference not set to an instance of an object.' :

سلام و عرض خسته نباشید در قسمت 65 بخش ساخت صفحه Services/Create زمانی که متد onPost رو فرا میخونه مقادیر پراپرتی CarServiceVM رو حذف میکنه و ارور null میده. کدها رو از فایل استاد کپی کردم بازم درست نشد فقط مقادیری که به صورت input در view وجود دارند مقدار دارند بقیه زمانی که کلید submit زده میشه حذف میشن

    public class CreateModel : PageModel
    {
        private ApplicationDbContext _db;

        public CreateModel(ApplicationDbContext db)
        {
            _db = db;
        }

        [BindProperty]
        public CarServiceViewModel CarServiceVM { get; set; }
        public async Task<IActionResult> OnGet(int carId)
        {
            CarServiceVM = new CarServiceViewModel()
            {
                Car = await _db.Cars.Include(c => c.ApplicationUser)
                    .FirstOrDefaultAsync(c => c.Id == carId),
                ServiceHeader = new ServiceHeader()
            };

            List<string> lstServiceTypeInShoppingCart = _db.ServiceShoppingCarts
                .Include(c => c.ServiceType)
                .Where(c => c.CarId == carId)
                .Select(c => c.ServiceType.Name)
                .ToList();

            IQueryable<ServiceType> lstServices = from s in _db.ServiceTypes
                                                  where !(lstServiceTypeInShoppingCart.Contains(s.Name))
                                                  select s;

            CarServiceVM.ServiceTypes = lstServices.ToList();
            CarServiceVM.ServiceShoppingCarts = _db.ServiceShoppingCarts.Include(c => c.ServiceType)
                .Where(c => c.CarId == carId)
                .ToList();
            CarServiceVM.ServiceHeader.TotalPrice = 0;

            foreach (var item in CarServiceVM.ServiceShoppingCarts)
            {
                CarServiceVM.ServiceHeader.TotalPrice += item.ServiceType.Price;
            }

            return Page();
        }


        public async Task<IActionResult> OnPost()
        {
            if (ModelState.IsValid)
            {
                CarServiceVM.ServiceHeader.Date = DateTime.Now;
                CarServiceVM.ServiceShoppingCarts = _db.ServiceShoppingCarts.Include(c => c.ServiceType)
                    .Where(c => c.CarId == CarServiceVM.Car.Id).ToList();
                foreach (var shop in CarServiceVM.ServiceShoppingCarts)
                {
                    CarServiceVM.ServiceHeader.TotalPrice += shop.ServiceType.Price;
                }

                CarServiceVM.ServiceHeader.CarId = CarServiceVM.Car.Id;
                _db.ServiceHeaders.Add(CarServiceVM.ServiceHeader);
                await _db.SaveChangesAsync();

                foreach (var shop in CarServiceVM.ServiceShoppingCarts)
                {
                    ServiceDetails details = new ServiceDetails()
                    {
                        ServiceHeaderId = CarServiceVM.ServiceHeader.Id,
                        ServiceName = shop.ServiceType.Name,
                        ServicePrice = shop.ServiceType.Price,
                        ServiceTypeId = shop.ServiceTypeId
                    };
                    _db.ServiceDetails.Add(details);
                }
                _db.ServiceShoppingCarts.RemoveRange(CarServiceVM.ServiceShoppingCarts);
                await _db.SaveChangesAsync();
                return RedirectToPage("/Cars/Index", new { userId = CarServiceVM.Car.UserId });
            }

            return Page();
        }
  • 1401/07/20
  • ساعت 10:12

سلام

در اولین فرصت نمونه دوره  را بررسی میکنم و اگر  مشکل داشت  سریع رفع و  اطلاع رسانی میکنم


  • 1401/08/18
  • ساعت 11:30

با عرض سلام خدمت شما،

و با اجازه از استاد مدائنی عزیز،

دوست عزیز، بنظر میرسد که شما از net 6. در پروژه تان استفاده کرده اید. از آنجاییکه ModelState.IsValid بصورت false برمی گرداند برای رفع این خطا به فایل MyAutoService.csproj بروید و در قسمت <PropertyGroup> ویژگی Nullable آن را کامنت کنید

 <--<Nullable>enable</Nullable>--!>


logo-enamadlogo-samandehi