2013-09-23 33 views
2

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

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

我有回传给控制器的日期。在控制器内部,我正在做一个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传递回视图,以便在表格中显示它。

回答

2

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

return View(productionData) 

,然后在视图中,您可以有@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; 
    } 
} 

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

@model ProductionDataViewModel 
0

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

如果ProductionData越来越值,那么

回报return View(ProductionData);

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

0

使用一个模型,是这样的:

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 
0

ProductionData变量现在应该IEnumerbable<tbl_dppITHrRow>类型。

您可以使用此代码在你行动的底部从控制器模型传递:

return View(ProductionData); 

在你看来,你可以把下列剃刀代码在您看来我们做这个模型类型。CSHTML文件:

@model IEnumerbable<tbl_dppITHrRow> 

然后,你可以用你的模型视图代码:

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