通过两种模式来查看 [英] pass two models to view
问题描述
我是新来的MVC,并尝试用它做一个小项目,以了解它。我这是为了显示特定日期的汇率和天气的页面。所以我应该通过货币模型和天气模式。我已经做了通过货币模型和工作正常,但我不知道如何通过第二种模式。和大多数的节目的教程如何传递只有一种模式。
你们可以给一个想法如何做到这一点。
这是我目前的控制器操作,它发送的货币模型
公众的ActionResult指数(INT年,月整型,诠释天)
{
VAR模型=从_db.Currenciesř
其中,r.date ==新的日期时间(年,月,日)
选择R; 返回查看(模型);
}
您可以创建特殊的视图模型包含两种型号:
公共类CurrencyAndWeatherViewModel
{
公共IEnumerable的<货币和GT;货币{获取;设置;}
公共气象CurrentWeather {获取;设置;}
}
和通过它来查看。
公众的ActionResult指数(INT年,月整型,诠释天)
{
VAR货币从_db.Currencies R =
其中,r.date ==新的日期时间(年,月,日)
选择R;
VAR天气= ... VAR模型=新CurrencyAndWeatherViewModel {货币= currencies.ToArray(),CurrentWeather =天气}; 返回查看(模型);
}
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);
}
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);
}
这篇关于通过两种模式来查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!