I have a asp.net MVC4 web project it shows a list of production data for that day. I have added a datetime picker which allows the user to select a date that they want to show information for.
我有一个asp.net MVC4 web项目,它显示了当天的生产数据列表。我添加了一个datetime picker,它允许用户选择一个他们想要显示信息的日期。
The problem i am having is i am not sure how to go about passing the information back to the view from the method i have inside the controller.
我遇到的问题是,我不知道如何从控制器内部的方法将信息传回视图。
I have the date passing back to the controller. Inside the controller i am doing a LINQ statement that allows me to select only the production data for that day.
我将日期传回控制器。在控制器中,我正在执行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();
I am looking to get the Var ProductionData passed back to the view so that display it inside a table.
我希望将Var ProductionData传回视图,以便在表中显示它。
4 个解决方案
#1
2
You can return ProductionData
directly to your View.
可以将ProductionData直接返回到视图。
return View(productionData)
And then in your View you could have @model IEnumerable<Type>
然后在你的视图中你可以有@model IEnumerable <类型> 。
However, a better practice would be to create a strongly typed ViewModel
to hold the ProductionData and then return the following:
但是,更好的做法是创建一个强类型的ViewModel来保存ProductionData,然后返回以下内容:
var model = new ProductionDataViewModel();
model.Load();
return View(model);
Where model
a definition as follows:
模型定义如下:
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;
}
}
Then in your view use the new strongly typed ViewModel:
然后在您的视图中使用新的强类型视图模型:
@model ProductionDataViewModel
#2
0
Use a model, something like:
使用一个模型,比如:
public class ProductionDataModel
{
//put your properties in here
public List<ProductionData> Data { get; set; }
}
Then create/return it in your ActionResult
:
然后在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);
Then in your view, set your model at the top:
然后在你的视图中,将你的模型放在顶部:
@model ProductionDataModel
#3
0
Your ProductionData
variable should now be of type IEnumerbable<tbl_dppITHrRow>
.
您的ProductionData变量现在应该是ienumable类型
You can pass in the model from your controller using this code at the bottom of your action:
您可以使用下面的代码在您的控制器中传递模型:
return View(ProductionData);
In your view, you can make this your model type by placing the following Razor code in your view's .cshtml file:
在您的视图中,您可以将以下剃刀代码放在视图的.cshtml文件中,使其成为您的模型类型:
@model IEnumerbable<tbl_dppITHrRow>
Then, you can use your model in your view code:
然后,您可以在视图代码中使用您的模型:
@foreach(var row in Model) {
<div>@row.Value</div>
}
#4
0
The problem here is that you are returning nothing to your view here return View();
this view just render view and no data will be passed to it.
这里的问题是您没有返回任何东西到您的视图返回视图();这个视图只是呈现视图,没有数据将被传递给它。
if ProductionData
is getting values then
如果ProductionData获取值
return return View(ProductionData);
返回返回视图(ProductionData);
You can then use the values passed in the view.
然后可以使用在视图中传递的值。
#1
2
You can return ProductionData
directly to your View.
可以将ProductionData直接返回到视图。
return View(productionData)
And then in your View you could have @model IEnumerable<Type>
然后在你的视图中你可以有@model IEnumerable <类型> 。
However, a better practice would be to create a strongly typed ViewModel
to hold the ProductionData and then return the following:
但是,更好的做法是创建一个强类型的ViewModel来保存ProductionData,然后返回以下内容:
var model = new ProductionDataViewModel();
model.Load();
return View(model);
Where model
a definition as follows:
模型定义如下:
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;
}
}
Then in your view use the new strongly typed ViewModel:
然后在您的视图中使用新的强类型视图模型:
@model ProductionDataViewModel
#2
0
Use a model, something like:
使用一个模型,比如:
public class ProductionDataModel
{
//put your properties in here
public List<ProductionData> Data { get; set; }
}
Then create/return it in your ActionResult
:
然后在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);
Then in your view, set your model at the top:
然后在你的视图中,将你的模型放在顶部:
@model ProductionDataModel
#3
0
Your ProductionData
variable should now be of type IEnumerbable<tbl_dppITHrRow>
.
您的ProductionData变量现在应该是ienumable类型
You can pass in the model from your controller using this code at the bottom of your action:
您可以使用下面的代码在您的控制器中传递模型:
return View(ProductionData);
In your view, you can make this your model type by placing the following Razor code in your view's .cshtml file:
在您的视图中,您可以将以下剃刀代码放在视图的.cshtml文件中,使其成为您的模型类型:
@model IEnumerbable<tbl_dppITHrRow>
Then, you can use your model in your view code:
然后,您可以在视图代码中使用您的模型:
@foreach(var row in Model) {
<div>@row.Value</div>
}
#4
0
The problem here is that you are returning nothing to your view here return View();
this view just render view and no data will be passed to it.
这里的问题是您没有返回任何东西到您的视图返回视图();这个视图只是呈现视图,没有数据将被传递给它。
if ProductionData
is getting values then
如果ProductionData获取值
return return View(ProductionData);
返回返回视图(ProductionData);
You can then use the values passed in the view.
然后可以使用在视图中传递的值。