MVC 4 how pass data correctly from controller to view

前端 未结 4 1315
长发绾君心
长发绾君心 2020-11-29 08:30

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 hom

相关标签:
4条回答
  • 2020-11-29 09:16

    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

    0 讨论(0)
  • 2020-11-29 09:17

    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
    
    0 讨论(0)
  • 2020-11-29 09:20

    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
    }
    
    0 讨论(0)
  • 2020-11-29 09:24

    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
      
    0 讨论(0)
提交回复
热议问题