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?
feedEventViewModel
is already declared as a single object, you can't declare it again as a List<FeedEventViewModel>()
. Other language such as Rust
allows you to "shadow" the variable declaration but C# not (and var
is just a shorter way to declare a variable).
You can solve this issue quite easily:
return View( new List<FeedEventViewModel>() {
new FeedEventViewModel{
AnimalId = command.AnimalId,
AnimalName = command.AnimalName,
FeederTypeId = command.FeederTypeId,
FeederType = command.FeederType
}
}
);