I am new to mvc and try to learn it by doing a small project with it. I have a page which is supposed to display that specific date's currencies and weather. so I should pass currencies model and weather model. I have done to pass currencies model and works fine but I dont know how to pass the second model. And most of the tutorials on the shows how to pass only one model.
can you guys give an idea how to do it.
this is my current controller action which sends currency model
public ActionResult Index(int year,int month,int day)
{
var model = from r in _db.Currencies
where r.date == new DateTime(year,month,day)
select r;
return View(model);
}
Introduction. In MVC we cannot pass multiple models from a controller to the single view.
Yes, you can use Tuple (brings magic in view having multiple model).
You can create special viewmodel that contains both models:
public class CurrencyAndWeatherViewModel
{
public IEnumerable<Currency> Currencies{get;set;}
public Weather CurrentWeather {get;set;}
}
and pass it to view.
public ActionResult Index(int year,int month,int day)
{
var currencies = from r in _db.Currencies
where r.date == new DateTime(year,month,day)
select r;
var weather = ...
var model = new CurrencyAndWeatherViewModel {Currencies = currencies.ToArray(), CurrentWeather = weather};
return View(model);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With