Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 591
Inkey Avatar asked Sep 04 '13 08:09

Inkey


People also ask

How can we pass the data from controller to view in MVC?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

Which method is used to pass data from controller to view?

ViewData, ViewBag, and TempData are used to pass data between controller, action, and views. To pass data from the controller to view, either ViewData or ViewBag can be used. To pass data from one controller to another controller, TempData can be used.

Can TempData be used to pass data from controller to view?

TempData is used to transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller. TempData stores the data temporarily and automatically removes it after retrieving a value. TempData is a property in the ControllerBase class.

Can we move data from controller to view using ViewData?

ViewBag. ViewBag is a very well known way to pass the data from Controller to View & even View to View. ViewBag uses the dynamic feature that was added in C# 4.0. We can say ViewBag=ViewData + Dynamic wrapper around the ViewData dictionary.


Video Answer


4 Answers

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
}
like image 59
Darren Avatar answered Oct 19 '22 20:10

Darren


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
like image 40
Satpal Avatar answered Oct 19 '22 22:10

Satpal


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
    
like image 2
Vadim Martynov Avatar answered Oct 19 '22 20:10

Vadim Martynov


Use models instead

var Melt
 Furnace1 =
       (from item in db.tbl_dppITHr
       where item.ProductionHour >= StartShift && item.ProductionHour <= EndDate
       select item).Sum(x => x.Furnace1Total),
return View("SomeVIew",MeltFurnace1)

In view@model "TypeOfMeltFurnace1"

You can reference model in view by property Model

like image 1
vborutenko Avatar answered Oct 19 '22 22:10

vborutenko