1

I am passing a DateTime value back from a view using [HttpPost] to the Controller. I have another method in the controller which i want to the result of the HttpPost method too. Or of i can pass the HttpPost back to the view.

What i want is to display the value of the LINQ in the form from the HttpPost method.

The original method that i use which populates the view is as follows.

  public ActionResult Index()
    {


        ViewBag.Message = "Real Time Production";

        DateTime ShiftStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
        DateTime StartShift = ShiftStart.AddHours(7);
        DateTime EndDate = StartShift.AddDays(1);
        try
        {
            var PumaProduct =
            new
        {
            PumaCastGood =
                (from item in db.tbl_dppITHr
                 where item.ProductionHour >= StartShift && item.ProductionHour <= EndDate
                 select item).Sum(x => x.PumaCastGross) ?? 0,

            PumaScrap =
                (from item in db.tbl_dppITHr
                 where item.ProductionHour >= StartShift && item.ProductionHour <= EndDate
                 select item).Sum(x => x.PumaScrap) ?? 0,

            PumaMachined =
            (
            from item in db.tbl_dppITHr
            where item.ProductionHour >= StartShift && item.ProductionHour <= EndDate
            select item).Sum(x => x.PumaMachined) ?? 0,

            PumaHeatTreat =
            (
            from item in db.tbl_dppITHr
            where item.ProductionHour >= StartShift && item.ProductionHour <= EndDate
            select item).Sum(x => x.ATIPuma) ?? 0,

            PumaShipped =
              (
            from item in db.tbl_dppITHr
            where item.ProductionHour >= StartShift && item.ProductionHour <= EndDate
            select item).Sum(x => x.PumaShipped) ?? 0,
        };
            ViewData["PumaCastGood"] = PumaProduct.PumaCastGood;
            ViewData["PumaCastScrap"] = PumaProduct.PumaScrap;
            ViewData["PumaMachined"] = PumaProduct.PumaMachined;
            ViewData["PumaShipped"] = PumaProduct.PumaShipped;
            ViewData["PumaHeatTreat"] = PumaProduct.PumaHeatTreat;

The following is the HttpPost method that i would like to pass to the ActionResult Index or pass to the view along with the item in Index method.

    [HttpPost]
    public ActionResult GetSigmaDateInfo(string dp)
    {
        DateTime SelectedDate = Convert.ToDateTime(dp);
        DateTime SelectedDateDayShiftStart = SelectedDate.AddHours(7);
        DateTime SelectedDateDayShiftEnd = SelectedDate.AddHours(19);

        var SigmaData =

            from n in db.tbl_dppITHr
            where n.ProductionHour >= SelectedDateDayShiftStart
            where n.ProductionHour <= SelectedDateDayShiftEnd
            select n;


        return View();

    }

I have tried the normal C# method of passing method values between methods.

Inkey
  • 2,189
  • 9
  • 39
  • 64

5 Answers5

2

Depends on what you're trying to do.

Redirection

Use Moho's answer above if your intention is to redirect between actions. Naturally this incurs a round trip to the browser. During this round trip your ViewBag will be lost, but you could always use TempData.

Transfer

If you want to simulate a redirect but without the round trip, you will have to write a tiny bit of custom code. See this article to learn how.

Return a different view

If your intention is to return the Index view from the GetSigmaDateInfo action, all you have to do is change the return statement at the end as follows:

return View("Index");  //Or substitute the name of the desired view

This avoids a second round trip to the browser, and preserves your ViewBag. The Index view can inspect any ViewBag values set by GetSigmaDateInfo, so there is no need to pass the data directly.

Code re-use

If there is more going on here (it's not entirely clear from your post), one proven technique is to refactor your code so that instead of two methods you have three methods. I'll explain.

Currently you have two actions, and you are thinking of the problem as one action calling another action. Instead, I suggest you think of it as two actions that do not have to call each other but which have logic in common.

What do you do when you have two functions that have logic in common? You refactor: remove the common logic, put it in a third function, and modify the two original functions to call that new function instead of having the logic in two places.

In this case the third function can be a new controller method (it must be marked private so that it cannot be called as an action) or you could consider writing a private helper class that contains the new function. Both are fine. I used the latter option in my example below.

public ActionResult Index()
{
    //Perform action-specific validation and logic here
    var result = DBHelper.GetSigmaData();  //Not sure what your intention is here
    ViewBag.SigmaData = result;
    //Do something with the result here
    return View();
}

and

[HttpPost]
public ActionResult GetSigmaDateInfo(string dp)
{
    //Perform action-specific validation and logic here
    var result = DBHelper.GetSigmaData(dp);
    ViewBag.SigmaData = result;
    //Do something with the result here
    return View();
}

and finally your common logic

static internal class DBHelper
{
    static public DateTime GetSigmaData(string dp = null)
    {
        DateTime SelectedDate = Convert.ToDateTime(dp);
        DateTime SelectedDateDayShiftStart = SelectedDate.AddHours(7);
        DateTime SelectedDateDayShiftEnd = SelectedDate.AddHours(19);

        var SigmaData =
            from n in db.tbl_dppITHr
            where n.ProductionHour >= SelectedDateDayShiftStart
            where n.ProductionHour <= SelectedDateDayShiftEnd
            select n;

        return SigmaData;
    }
}

Using this approach you avoid modifying the Index method to accept an optional argument. You probably don't want to do that. If you change the Index method you change the types of calls that you expose to the end user.

Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80
1

Are you trying to pass the value of SigmaData calculated in GetSigmaDateInfo to the Index method? If that is what you are trying to do then something along these lines should work.

public ActionResult Index(SigmaData SigmaData = null)
{
    if(SigmaData == null){
        //Handle the case where the call is coming straight from routing engine
    }else{
        //Handle the case where the call is coming from GetSigmaDateInfo()
    }
    //Code common to both cases
    return view("GetSigmaDateInfo");
}

[HttpPost]
public ActionResult GetSigmaDateInfo(string dp)
{
    DateTime SelectedDate = Convert.ToDateTime(dp);
    DateTime SelectedDateDayShiftStart = SelectedDate.AddHours(7);
    DateTime SelectedDateDayShiftEnd = SelectedDate.AddHours(19);

    var SigmaData =
        from n in db.tbl_dppITHr
        where n.ProductionHour >= SelectedDateDayShiftStart
        where n.ProductionHour <= SelectedDateDayShiftEnd
        select n;
    return Index(SigmaData);

}

If I got something wrong let me know in comments and I will try to fix the code from there. On a related note, I urge you to think about using strongly typed views as opposed to sending the information in ViewBag or ViewData.

JTMon
  • 3,189
  • 22
  • 24
0

Just put this data into ViewBag or ViewData, and use it in the view and done:

Controller:

[HttpPost]
public ActionResult GetSigmaDateInfo(string dp)
{
    DateTime SelectedDate = Convert.ToDateTime(dp);
    DateTime SelectedDateDayShiftStart = SelectedDate.AddHours(7);
    DateTime SelectedDateDayShiftEnd = SelectedDate.AddHours(19);

    var SigmaData =
        from n in db.tbl_dppITHr
        where n.ProductionHour >= SelectedDateDayShiftStart
        where n.ProductionHour <= SelectedDateDayShiftEnd
        select n;

    ViewBag.SigmaData = SigmaData;


    return View();
}

View:

@if(ViewBag.SigmaData != null)
{
  //Show the value somewhere in the view
}
Fals
  • 6,813
  • 4
  • 23
  • 43
0

call index view pass TempData["SigmaData"] = SigmaData;

return View("Index");

in view check if SigmaData is null or not

@if(TempData["SigmaData"] != null)
{
  //your POST method code
}
else
{
  //your GET method code
}
Nirmal
  • 924
  • 4
  • 9
0

Have Index() take an optional argument

public ActionResult Index( IEnumerable<tbl_dppITHr> p = null )
{
    // existing code

    if( null != p )
    {
        // new code to hand the case then you pass the parm from GetSigmaDateInfo(...)
    }

    // maybe more existing code
}

Then alter GetSigmaDateInfo to return a RedirectToAction result w/ the parameter in tow

[HttpPost]
public ActionResult GetSigmaDateInfo(string dp)
{
    DateTime SelectedDate = Convert.ToDateTime(dp);
    DateTime SelectedDateDayShiftStart = SelectedDate.AddHours(7);
    DateTime SelectedDateDayShiftEnd = SelectedDate.AddHours(19);

    var SigmaData =
        from n in db.tbl_dppITHr
        where n.ProductionHour >= SelectedDateDayShiftStart
        where n.ProductionHour <= SelectedDateDayShiftEnd
        select n;

    return RedirectToAction( "Index", new { p = SigmaData.ToList() } );
}
Moho
  • 15,457
  • 1
  • 30
  • 31