c# - ASP.NET MVC 传递 DateTime 来查看

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

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

我遇到的问题是我不确定如何从 Controller 内部的方法将信息传递回 View 。

我将日期传回 Controller 。在 Controller 内部,我正在执行一个 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 传递回 View ,以便将其显示在表格中。

最佳答案

您可以返回ProductionData直接到您的 View 。

 return View(productionData)

然后在你的 View 中你可以有 @model IEnumerable<Type>

但是,更好的做法是创建强类型 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;
   }
}

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

 @model ProductionDataViewModel

关于c# - ASP.NET MVC 传递 DateTime 来查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18955067/

相关文章:

javascript - 为什么我的字符串返回 "&#39;"

c# - 将 ViewModel 绑定(bind)到 Listview 的问题

c# - 为什么 ASP.NET Identity 返回 401 而不是重定向到登录页面?

c# - 我可以使用 Func<object[], object> 委托(delegate)任何类型的参数 input 和 TResult 吗?

c# - 如何用图片替换asp.net/c中的随机数?

.net - vb.net 中的精确数字

asp.net - 在 Azure 上部署网站时未加载字体

c# - 在 XBAP 应用程序中包含外部文件

c# - 使用 OpenXML SDK 将 RTF 文件的内容嵌入到 DOCX 文件中

c# - 尝试调试一些旨在通过字符串变量将网页转换为PDF文档的iTextSharp代码