加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 传递两个模型来查看

发布时间:2020-12-16 01:21:58 所属栏目:百科 来源:网络整理
导读:参见英文答案 ASP.NET MVC – View with multiple models????????????????????????????????????4个 我是mvc的新手,并尝试用它做一个小项目来学习它.我有一个页面应该显示特定日期的货币和天气.所以我应该通过货币模型和天气模型.我已经完成了传递货币模型并
参见英文答案 > ASP.NET MVC – View with multiple models????????????????????????????????????4个
我是mvc的新手,并尝试用它做一个小项目来学习它.我有一个页面应该显示特定日期的货币和天气.所以我应该通过货币模型和天气模型.我已经完成了传递货币模型并且工作正常,但我不知道如何通过第二个模型.大多数教程都展示了如何只传递一个模型.

你们能不知道该怎么做.

这是我当前的控制器动作,它发送货币模型

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

解决方法

您可以创建包含两个模型的特殊视图模型:

public class CurrencyAndWeatherViewModel
{
   public IEnumerable<Currency> Currencies{get;set;}
   public Weather CurrentWeather {get;set;}
}

并传递给视图.

public ActionResult Index(int year,int day)
{
    var currencies = from r in _db.Currencies
                where r.date == new DateTime(year,day)
                select r;
    var weather = ...

    var model = new CurrencyAndWeatherViewModel {Currencies = currencies.ToArray(),CurrentWeather = weather};

    return View(model);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读