mrcbyk5353
Hectopat
- Katılım
- 7 Mart 2021
- Mesajlar
- 66
Daha fazla
- Cinsiyet
- Erkek
Merhaba.
Amacım sayfada scooter veri tabanından kullanıcıya dropdownlist ile scooter adlarını listeleyip kullanıcının scooterlardan bir tanesini ve tarihi seçip butona bastığında reservation veri tabanına kaydetmek istiyorum. Scooter adlarını sayfada listeleniyor ama butona bastığında aşağıda belirtmiş olduğum hatayı alıyorum. Yanlış yapılan yer neresidir acaba? Yardımcı olabilir misiniz?
Invalidoperationexception: The viewdata item that has the key 'scooter' is of type 'System. String' but must be of type 'ıenumerable<selectlistıtem>'.
Controller;
Dropdownlist ile bilgileri çektiğim Scooter Model;
Reservation verileri kaydetmek istediğim model;
.cshtml Kodum;
Amacım sayfada scooter veri tabanından kullanıcıya dropdownlist ile scooter adlarını listeleyip kullanıcının scooterlardan bir tanesini ve tarihi seçip butona bastığında reservation veri tabanına kaydetmek istiyorum. Scooter adlarını sayfada listeleniyor ama butona bastığında aşağıda belirtmiş olduğum hatayı alıyorum. Yanlış yapılan yer neresidir acaba? Yardımcı olabilir misiniz?
Invalidoperationexception: The viewdata item that has the key 'scooter' is of type 'System. String' but must be of type 'ıenumerable<selectlistıtem>'.
Controller;
C#:
using BusinessLayer.Concrete;
using DataAccessLayer.EntityFramework;
using EntityLayer.Concrate;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace MyScooter.Areas.Member.Controllers
{
[Area("Member")]
public class ReservationController : Controller
{
ScooterManager scooterManager = new ScooterManager(new EfScooterDal());
ReservationManager reservationManager = new ReservationManager(new EfReservationDal());
public IActionResult MyCurrentReservation()
{
return View();
}
public IActionResult MyOldReservation()
{
return View();
}
[HttpGet]
public IActionResult NewReservation()
{
List<SelectListItem> values = (from x in scooterManager.TGetList()
select new SelectListItem
{
Text = x.ScooterName,
Value = x.ScooterID.ToString()
}).ToList();
ViewBag.ScooterList = values;
return View();
}
[HttpPost]
public IActionResult NewReservation( Reservation p)
{
if (!ModelState.IsValid)
{
return View(p);
}
p.AppUserId = 5;
p.Status = "Onay Bekliyor";
reservationManager.TAdd(p);
return RedirectToAction("MyCurrentReservation");
}
}
}
Dropdownlist ile bilgileri çektiğim Scooter Model;
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityLayer.Concrate
{
public class Scooter
{
[Key]
public int ScooterID { get; set; }
public string ScooterName { get; set; }
public string Color { get; set; }
public double EnginPower { get; set; }
public string Image { get; set; }
public string CreatedYear { get; set; }
public int Capacity { get; set; }
public bool Status { get; set; }
}
}
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityLayer.Concrate
{
public class Reservation
{
[Key]
public int ReservationID { get; set; }
public int AppUserId { get; set; }
public AppUser AppUser { get; set; }
public string Scooter { get; set; }
public DateTime ReservationDate { get; set; }
public string Status { get; set; }
}
}
.cshtml Kodum;
C#:
@model EntityLayer.Concrate.Reservation
@{
ViewData["Title"] = "NewReservation";
Layout = "~/Views/Shared/_UserLayout.cshtml";
}
<h1>Yeni Rezervasyon</h1>
<form method="post">
<div class="form-group">
@Html.Label("Scooter")
@Html.DropDownListFor(x => x.Scooter, (IEnumerable<SelectListItem>)ViewBag.ScooterList, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.Label("Tarih")
<input type="date" asp-for="ReservationDate" class="form-control"/>
</div>
<button class="btn btn-outline-success"> Rez Yap</button>
</form>
Son düzenleyen: Moderatör: