传递两个模型查看 [英] pass two models to view

查看:25
本文介绍了传递两个模型查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 mvc 的新手,并尝试通过用它做一个小项目来学习它.我有一个页面应该显示该特定日期的货币和天气.所以我应该通过货币模型和天气模型.我已经完成了通过货币模型并且工作正常,但我不知道如何通过第二个模型.而且大部分教程都展示了如何只通过一个模型.

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;}
}

并将其传递给查看.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆