【问题标题】:ASP.NET MVC Passing DataTime to View aASP.NET MVC 传递 DateTime 来查看
【发布时间】:2013-09-23 08:45:09
【问题描述】:

我有一个 asp.net MVC4 web 项目,它显示了当天的生产数据列表。我添加了一个日期时间选择器,允许用户选择他们想要显示信息的日期。

我遇到的问题是我不确定如何从控制器内部的方法将信息传递回视图。

我将日期传回给控制器。在控制器内部,我正在执行一个 LINQ 语句,允许我只选择当天的生产数据。

  [HttpPost]
    public ActionResult GetProductionDateInfo(string dp)
    {
        DateTime SelectedDate = Convert.ToDateTime(dp);
        DateTime SelectedDateDayShiftStart = SelectedDate.AddHours(7);
        DateTime SelectedDateDayShiftEnd = SelectedDate.AddHours(19);

        var ProductionData =

            from n in db.tbl_dppITHr
            where n.ProductionHour >= SelectedDateDayShiftStart
            where n.ProductionHour <= SelectedDateDayShiftEnd
            select n;


        return View();

我希望将 Var ProductionData 传递回视图,以便在表格中显示它。

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    您可以将ProductionData 直接返回到您的视图。

     return View(productionData)
    

    然后在你的视图中你可以有@model IEnumerable&lt;Type&gt;

    但是,更好的做法是创建一个强类型 ViewModel 来保存 ProductionData,然后返回以下内容:

     var model = new ProductionDataViewModel();
     model.Load();
    
     return View(model);
    

    其中model的定义如下:

    public class ProductionDataViewModel { 
    
       public List<ProductionDataType> ProductionData { get; set; }
       public void Load() {
           ProductionData = from n in db.tbl_dppITHr
            where n.ProductionHour >= SelectedDateDayShiftStart
            where n.ProductionHour <= SelectedDateDayShiftEnd
            select n;
       }
    }
    

    然后在您的视图中使用新的强类型 ViewModel:

     @model ProductionDataViewModel
    

    【讨论】:

      【解决方案2】:

      使用模型,例如:

      public class ProductionDataModel
      {
          //put your properties in here
      
          public List<ProductionData> Data { get; set; }
      }
      

      然后在您的ActionResult 中创建/返回它:

      var ProductionData =
          from n in db.tbl_dppITHr
          where n.ProductionHour >= SelectedDateDayShiftStart
          where n.ProductionHour <= SelectedDateDayShiftEnd
          select new ProductionData
          {
              //set properties here
          };
      
      var model = new ProductionDataModel
      {
          Data = ProductionData
      };
      
      
      return View(model);
      

      然后在您的视图中,将您的模型设置在顶部:

      @model ProductionDataModel
      

      【讨论】:

        【解决方案3】:

        您的ProductionData 变量现在应该是IEnumerbable&lt;tbl_dppITHrRow&gt; 类型。

        您可以使用操作底部的以下代码从控制器传入模型:

        return View(ProductionData);
        

        在您的视图中,您可以通过将以下 Razor 代码放置在视图的 .cshtml 文件中来使其成为您的模型类型:

        @model IEnumerbable<tbl_dppITHrRow>
        

        然后,您可以在视图代码中使用您的模型:

        @foreach(var row in Model) {
            <div>@row.Value</div>
        }
        

        【讨论】:

          【解决方案4】:

          这里的问题是你没有返回任何东西到你的视图return View();这个视图只是渲染视图并且没有数据会被传递给它。

          如果ProductionData 正在获取值,那么

          返回return View(ProductionData);

          然后您可以使用视图中传递的值。

          【讨论】:

            猜你喜欢
            • 2016-01-01
            • 2023-04-06
            • 1970-01-01
            • 1970-01-01
            • 2014-08-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多