C# 传递两个模型查看

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17030399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:24:34  来源:igfitidea点击:

pass two models to view

c#asp.net-mvcasp.net-mvc-4model

提问by Arif YILMAZ

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.

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

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

采纳答案by Kirill Bestemyanov

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

回答by Justin Bicknell

It sounds like you could use a model that is specific to this view.

听起来您可以使用特定于此视图的模型。

public class MyViewModel{

  public List<Currencies> CurrencyList {get;set;}

}

and then from your controller you could pass this new View Model into the view instead:

然后从您的控制器中,您可以将这个新的视图模型传递到视图中:

    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(new MyViewModel { CurrencyList = model.ToList() });
    }

You can than just add more properties to your view model which contain any other models (Weather model) and set them appropriately.

您不仅可以向包含任何其他模型(天气模型)的视图模型添加更多属性并适当地设置它们。

回答by nesimtunc

You have to create a new model which has to contain the whole objects that you want to pass it to view. You should create a model (class, object) which inherits the base model (class, object).

您必须创建一个新模型,该模型必须包含要传递给视图的整个对象。您应该创建一个继承基础模型(类、对象)的模型(类、对象)。

And other suggestion you may send objects (models) via View["model1"] and View["model2"] or just an array that contains objects to pass it and cast them inside the view which I don't advise .

和其他建议,您可以通过 View["model1"] 和 View["model2"] 或仅包含对象的数组发送对象(模型)以传递它并将它们投射到我不建议的视图中。