asp.net-core

InvalidOperationException: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'PricingID'


I'm trying to post car reservation and although all values posting when I debugged but still get error.

I also checked network in developer tools and all values seen in payload. But I get an error about PricingID like

There is no ViewData item of type 'IEnumerable' that has the key 'PricingID'

public async Task<IActionResult> Index(int id)
{
    ViewBag.v1 = "Rent A Car";
    ViewBag.v2 = "Rent A Car Form";
    ViewBag.v3 = id;

    var client = _httpClientFactory.CreateClient();
    var responseMessage = await client.GetAsync("https://localhost:44380/api/Location");

    var jsonData = await responseMessage.Content.ReadAsStringAsync();
    var values = JsonConvert.DeserializeObject<List<ResultLocationDto>>(jsonData);

    List<SelectListItem> values2 = (from x in values
                                    select new SelectListItem
                                           {
                                               Text = x.Name,
                                               Value = x.Id.ToString()
                                           }).ToList();
    ViewBag.v = values2;

    var responseMessage2 = await client.GetAsync("https://localhost:44380/api/Pricing");
    var jsonData2 = await responseMessage2.Content.ReadAsStringAsync();
    var pricings = JsonConvert.DeserializeObject<List<ResultPricingDto>>(jsonData2);

    List<SelectListItem> values3 = (from x in pricings
                                    select new SelectListItem
                                           {
                                               Text = x.Name,
                                               Value = x.Id.ToString()
                                           }).ToList();
    ViewBag.pricing = values3;

    return View();
}

View:

@model CreateReservationDto
@{
    ViewData["Title"] = "Index";
    Layout = "~/Views/UILayout/Index.cshtml";
}

<div class="col-md-12 block-9 mb-md-5"> 
    <form class="bg-light p-5 contact-form" method="post">
        <h3 class="alert alert-success">Car Reservation Form</h3>
        <br />
            <input type="hidden" asp-for="CarID" value="@ViewBag.v3" class="form-control" placeholder="Your Car">
        <div class="form-group">
            <input type="text" asp-for="Name" class="form-control" placeholder="Name">
        </div>
        <div class="form-group">
            <input type="text" asp-for="Surname" class="form-control" placeholder="Surname">
        </div>
        <div class="form-group">
            <input type="text" asp-for="Email" class="form-control" placeholder="Email">
        </div>
        <div class="form-group">
            <input type="text" asp-for="Phone" class="form-control" placeholder="Phone">
        </div>
        <div class="form-group">
            @Html.DropDownListFor(x => x.PricingID, (List<SelectListItem>)ViewBag.pricing, new { @class = "form-control" })
        </div>
        <div class="form-group">
            @Html.DropDownListFor(x => x.PickUpLocationID, (List<SelectListItem>)ViewBag.v, new { @class = "form-control" })
        </div>
        <div class="form-group">
            @Html.DropDownListFor(x=>x.DropOffLocationID,(List<SelectListItem>)ViewBag.v,new{@class="form-control"})
        </div>
        <div class="form-group">
            <input type="text" asp-for="Age" class="form-control" placeholder="Age">
        </div>
        <div class="form-group">
            <input type="text" asp-for="DriverLicenseYear" class="form-control" placeholder="Driver License Year">
        </div>
        <div class="form-group">
            <textarea  asp-for="Description" id="" cols="30" rows="7" class="form-control" placeholder="Your requests"></textarea>
        </div>
        <div class="form-group">
            <input type="submit" value="Send Reservation" class="btn btn-primary py-3 px-5">
        </div>
    </form>
</div>

I can post it by swagger correctly.


Solution

  • Problem solved!

    I had a status property in CreateReservationDto and I fill it as default normally so I forgot to remove this property in dto.