I'm trying to convert a view model to list and then return it to the view but am getting the cannot implicity convert type error.
Code:
public ActionResult Index(FeedEventCommand command)
{
var feedEventViewModel = new FeedEventViewModel
{
AnimalId = command.AnimalId,
AnimalName = command.AnimalName,
FeederTypeId = command.FeederTypeId,
FeederType = command.FeederType
};
feedEventViewModel = new List<feedEventViewModel>(); <--Error line
return View(feedEventViewModel);
}
What am I doing wrong in this case?
feedEventViewModelis already declared as a single object, you can't declare it again as aList<FeedEventViewModel>(). Other language such asRustallows you to "shadow" the variable declaration but C# not (andvaris just a shorter way to declare a variable).You can solve this issue quite easily: