InvalidOperationException: The ViewData item that has the key 'WeatherLocation.LocationId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
RSS
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);
}
Member
411 Points
1327 Posts
InvalidOperationException: The ViewData item that has the key 'WeatherLocation.LocationId' is of...
Jun 04, 2020 03:32 PM|polachan|LINK
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
View
All-Star
53041 Points
23612 Posts
Re: InvalidOperationException: The ViewData item that has the key 'WeatherLocation.LocationId' is...
Jun 04, 2020 05:08 PM|mgebhard|LINK
The error indicates the select options are empty. The post action does not populate the Locations property.