MVC 4 how pass data correctly from controller to view

I currently have a controller with a LINQ statement that i am passing data from to my view. I am trying to find a more efficient and better coding method to do this. My home controller statement is as follows.

Var Melt
  Furnace1 =
           (from item in db.tbl_dppITHr
           where item.ProductionHour >= StartShift && item.ProductionHour <= EndDate
           select item).Sum(x => x.Furnace1Total),

ViewData["Furnace1Total"] = Melt.Furnace1;

In my view i then reference the ViewData To show this. Using

 @model dynamic

Now i have quite alot of linq statements inside the Index method. And for each one i am doing the ViewData[]

I am hoping that someone can show how i pass more than one var from a controller across to a view without the ViewData or ViewBag methods. And how i would get access to this within my view.


Solution 1:

You should create a ViewModel with all of your data needed and then pass that down to the view.

public class ViewModel 
{
   public List<int> Melt1 { get; set; }

   public void LoadMeltProperties() 
   {

       if (Melt1 == null) 
       {
          Melt1 = new List<int>();
       }

       Melt1 = (from item in db.tbl_dppITHr
       where item.ProductionHour >= StartShift && item.ProductionHour <= EndDate
       select item).Sum(x => x.Furnace1Total).ToList();
   }

   public ViewModel Load()
   {
       LoadMeltProperties();
       return this;
   }
}

public ActionResult YourControllerAction() 
{
      var vm = new ViewModel().Load();
      return View("ViewName", vm);
}

Then in your View you can use a strongly typed model rather than dynamic

@model ViewModel

You can then iterate over your ViewModel properties via:

foreach(var melt in Model.Melt1) {
     // do what you require
}

Solution 2:

IMHO, you should create a ViewModel an pass data using it.

Create a class

public class MyViewModel
{
    public <MeltFurnace1Type> MeltFurnace1{get;set;}
}

In Action Method

public ActionResult Action() 
{
      MyViewModel vm = new MyViewModel();
      vm.MeltFurnace1 = something;
      return View("YourViewName", vm);
}

In View

@model MyViewModel

//You can access your property using
Model.MeltFurnace1

Solution 3:

If you need to pass data actually from the controller and its data is depend on internal state or input controller parameters or has other properties of "business data" you should use Model part from MVC pattern:

Model objects are the parts of the application that implement the logic for the application's data domain. Often, model objects retrieve and store model state in a database. For example, a Product object might retrieve information from a database, operate on it, and then write updated information back to a Products table in a SQL Server database.

You can see details here or look to the Models and Validation in ASP.NET MVC part of Microsoft tutorial.

  1. Add model class:

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string City { get; set; }
    }
    
  2. Pass model object to the view:

    public ActionResult Index()
    {
        var model = GetModel();
        return View(model);
    }
    
  3. Add strongly typed View via define model type:

    @model Person
    
  4. Use Model variable in your view:

    @Model.City