C# MVC 4 如何正确地将数据从控制器传递到视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18608452/
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 12:43:34  来源:igfitidea点击:

MVC 4 how pass data correctly from controller to view

c#.netasp.net-mvclinqasp.net-mvc-4

提问by Inkey

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.

我目前有一个带有 LINQ 语句的控制器,我正在将数据从它传递到我的视图。我试图找到一种更有效、更好的编码方法来做到这一点。我的家庭控制器声明如下。

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

在我看来,我然后引用 ViewData 来显示这一点。使用

 @model dynamic

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

现在我在 Index 方法中有很多 linq 语句。对于每一个我正在做的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.

我希望有人可以展示我如何在没有 ViewData 或 ViewBag 方法的情况下将多个变量从控制器传递到视图。以及我如何在我看来访问它。

采纳答案by Darren

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

您应该ViewModel使用所有需要的数据创建一个,然后将其传递给视图。

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 typedmodel rather than dynamic

然后在您的视图中,您可以使用strongly typed模型而不是dynamic

@model ViewModel

You can then iterate over your ViewModel properties via:

然后,您可以通过以下方式遍历您的 ViewModel 属性:

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

回答by vborutenko

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"

在视图中@model "TypeOfMeltFurnace1"

You can reference model in view by property Model

您可以通过属性模型在视图中引用模型

回答by Satpal

IMHO, you should create a ViewModelan pass data using it.

恕我直言,您应该ViewModel使用它创建一个通行证数据。

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

回答by Vadim Martynov

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:

如果您需要从控制器实际传递数据并且其数据取决于内部状态或输入控制器参数或具有“业务数据”的其他属性,则应使用MVC 模式中的模型部分:

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.

模型对象是应用程序的一部分,用于实现应用程序数据域的逻辑。通常,模型对象在数据库中检索和存储模型状态。例如,Product 对象可能从数据库中检索信息,对其进行操作,然后将更新的信息写回到 SQL Server 数据库中的 Products 表中。

You can see details hereor look to the Models and Validation in ASP.NET MVCpart of Microsoft tutorial.

您可以在此处查看详细信息或查看Microsoft 教程的ASP.NET MVC部分中的模型和验证

  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 Viewvia define model type:

    @model Person
    
  4. Use Modelvariable in your view:

    @Model.City
    
  1. 添加模型类:

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string City { get; set; }
    }
    
  2. 将模型对象传递给视图:

    public ActionResult Index()
    {
        var model = GetModel();
        return View(model);
    }
    
  3. 通过定义模型类型添加强类型视图

    @model Person
    
  4. Model在您的视图中使用变量:

    @Model.City