Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

InvalidOperationException: The ViewData item that has the key 'WeatherLocation.LocationId' is of type 'System.Int32' but must be of type 'IEnumerable'.

$
0
0

Please help. When I am trying to poppulate the  data  in the same view ina table it always show the error InvalidOperationException: The ViewData item that has the key 'WeatherLocation.LocationId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'..

In Post method it shows the error 

InvalidOperationException: The ViewData item that has the key 'WeatherLocation.LocationId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
AspNetCore.Areas_Customer_Views_Forcast_Index.<ExecuteAsync>b__27_0() in Index.cshtml
+
@Html.DropDownListFor(m => m.WeatherLocation.LocationId, Model.Locations, "-Select a Location",

This error is coming when I poppulate the data on the table after I clicked the submit button and calling its post method. something error is coming in WeatherLocation please help

I am trying to list the record in the same view Index. It is working and the data is coming on the query but when I am trying to list the record on the index view  from post method , it is showing some error on the drop down of location . Please help

controller

 public async Task<IActionResult> Index()
        {
            IEnumerable<WeatherLocation> locList = _unitOfWork.Location.GetAll();
            ViewBag.LocationName = _unitOfWork.Location.GetAll();

            WeatherVM weatherVM = new WeatherVM()

            {

                Locations = locList.Select(i => new SelectListItem
                {
                    Text = i.LocationName,
                    Value = i.LocationId.ToString()
                }),
                WeatherLocation = new WeatherLocation()
            };

            weatherVM.ForcastDate= DateTime.UtcNow.Date;

            return View(weatherVM);
        }
        
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Index(WeatherVM weatherVM)
        {
            IEnumerable<WeatherLocation> locList = _unitOfWork.Location.GetAll();
            int locationId = weatherVM.WeatherLocation.LocationId;
            var LocationObj = _unitOfWork.Location.
                        GetFirstOrDefault(l => l.LocationId == locationId);

            string _location = weatherVM.LocationName;
            string woied = LocationObj.Woeid;
            string forcastDate = weatherVM.ForcastDate.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);
           // DateTime.Today.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
            if (_location == "B")
            {
                woied = "44544";
            }

            var weatherList = await _weatherRepository.GetWeatherAsync(woied,  forcastDate);
            
            weatherVM.Weather = weatherList;
            return View(weatherVM);
        }

View

@model MetaWeather.ViewModels.WeatherVM
@{
    ViewData["Title"] = "Index";
}<h1>Forcast Weather </h1><form asp-controller="Forcast" asp-action="Index" method="post"><div class="form-group"><div class="row"><div class="col-sm-2"><label asp-for="Locations">Locations</label></div><div class="col-4"><select class="form-control" asp-for="LocationName" data-role="select" selected="selected" data-parsley-errors-container="#errId17"><option value="">-- Select --</option><option value="B">Belfast</option><option value="L">London</option><option value="O">Other</option></select><span asp-validation-for="LocationName" class="text-danger"></span></div></div></div><div class="form-group"><div class="row"><div class="col-sm-2"><label asp-for="Locations">Locations</label></div><div class="col-4">
                @Html.DropDownListFor(m => m.WeatherLocation.LocationId, Model.Locations, "-Select a Location",
              new { @class = "form-control" })</div></div></div><div class="form-group row"><div class="col-sm-2"><label asp-for="ForcastDate"></label></div><div class="col-4"><input type='text' id="forcastDate" asp-for="ForcastDate" class="form-control" /><span asp-validation-for="ForcastDate" class="text-danger"></span></div><br /></div><div class="form-group row"><div class="col-4 offset-4"><button type="submit" class="btn btn-primary form-control">
                View</button></div></div><hr /><table>
        @if (Model.Weather == null)
        {<tr><td colspan="3" class="text-center">No Forcast Listed</td></tr>

        }
        else
        {
            <tr><th>ApplicableDate</th><th>WeatherStateName</th><th>WeatherStateAbbr</th><th>WindDirectionCompass</th><th>CreatedDate</th><th>MinTemp</th><th>MaxTemp</th><th>TheTemp</th><th>WindSpeed</th><th>windDirection</th><th>AirPressure</th><th>Humidity</th><th>Visibllity</th><th>Predictability</th><th></th></tr>
            @foreach (var item in Model.Weather)
            {<tr><td>@String.Format("{0:dd/MM/yyyy}", item.applicable_date)</td><td>@item.weather_state_name</td></tr>
            }
        }</table></form>
@section Scripts{<script>$(document).ready(function () {$("#forcastDate").datepicker({ format: 'dd/mm/yyyy', date: new Date(1900, 01, 01), autoclose: true, todayBtn: 'linked' });

        });
    </script>
}


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>