2

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.

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.

  [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.

4 Answers 4

2

You can return ProductionData directly to your View.

 return View(productionData)

And then in your View you could have @model IEnumerable<Type>

However, a better practice would be to create a strongly typed ViewModel to hold the ProductionData and then return the following:

 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
Sign up to request clarification or add additional context in comments.

Comments

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:

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

Comments

0

Your ProductionData variable should now be of type IEnumerbable<tbl_dppITHrRow>.

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:

@model IEnumerbable<tbl_dppITHrRow>

Then, you can use your model in your view code:

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

Comments

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

return return View(ProductionData);

You can then use the values passed in the view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.