我有一个 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/