17

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);
    }
tereško
  • 58,060
  • 25
  • 98
  • 150
Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189

3 Answers3

44

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);
}
Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38
6

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 .

nesimtunc
  • 849
  • 7
  • 28
  • how do u send them with View ? how do you pass it from controller to view – Arif YILMAZ Jun 10 '13 at 18:40
  • In Controller class `ViewData["CurrentTime"] = DateTime.Now.ToString();` and in view page `
    <%: ViewData["CurrentTime"] %>
    ` for more details this link may help [link](http://www.dotnetfunda.com/articles/article1310-how-to-pass-data-from-controllers-to-views-in-aspnet-mvc-tutorial-no-2.aspx)
    – nesimtunc Jun 10 '13 at 18:43
3

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.

Justin Bicknell
  • 4,804
  • 18
  • 26