2013-10-01 150 views
1

我將使用[HttpPost]的DateTime值從視圖傳回控制器。我在控制器中有另一種方法,我想要HttpPost方法的結果。或者我可以將HttpPost傳遞迴視圖。將控制器方法中的一個項目傳遞給同一控制器中的另一個方法

我想要的是從HttpPost方法的窗體中顯示LINQ的值。

我用它填充視圖的原始方法如下。

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; 

以下是我想傳遞給的ActionResult指數或通過以與指數方法的項目沿視圖中的HttpPost方法。

[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(); 

    } 

我已經嘗試了方法之間傳遞方法值的正常C#方法。

+0

SigmaData包含什麼? – Nirmal

回答

2

取決於你想做什麼。

重定向

使用莫霍面的回答上面,如果你的目的是行動之間重定向。當然這會導致瀏覽器的往返旅程。在這次往返旅途中,您的ViewBag將會丟失,但您始終可以使用TempData。

轉移

如果要模擬一個重定向,但沒有往返,你就必須編寫的自定義代碼一點點。瞭解如何使用this article

返回了不同的看法

如果你的意圖是從GetSigmaDateInfo動作返回索引視圖,所有你需要做的是改變return語句在結尾如下:

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

這避免第二次往返瀏覽器,並保留ViewBag。 Index視圖可以檢查由GetSigmaDateInfo設置的任何ViewBag值,因此不需要直接傳遞數據。

代碼複用

如果有更多的事情在這裏(這不是從您的帖子完全清楚),一個成熟的技術,這樣的而不是兩種方法你有三種方法來重構你的代碼。我會解釋。

目前您有兩個操作,並且您正在考慮將該問題作爲一個操作調用另一個操作。相反,我建議你把它看作兩個不需要彼此調用但是有共同邏輯的行爲。

當你有兩個具有共同邏輯的函數時,你會做什麼?你重構:刪除公共邏輯,把它放在第三個函數中,修改兩個原始函數來調用這個新函數,而不是在兩個地方有邏輯。

在這種情況下,第三功能可以是一個新的控制器方法(必須註明私人所以它不能被稱爲一個動作),或者你可以考慮編寫包含新功能的私人助手類。兩者都很好。我在下面的示例中使用了後一個選項。

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(); 
} 

[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(); 
} 

最後您共同邏輯

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; 
    } 
} 

使用這種方法可以避免修改索引方法接受一個可選的參數。你可能不想這樣做。如果更改索引方法,則可以更改公開給最終用戶的調用類型。

0

只要把這些數據轉化爲ViewBag的ViewData,並在視圖中使用它,並完成:

控制器:

[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(); 
} 

查看:

@if(ViewBag.SigmaData != null) 
{ 
    //Show the value somewhere in the view 
} 
0

調用索引查看通過TempData [「SigmaData」] = SigmaData;

return View("Index"); 

鑑於檢查,如果SigmaData爲空或不是

@if(TempData["SigmaData"] != null) 
{ 
    //your POST method code 
} 
else 
{ 
    //your GET method code 
} 
1

你們是不是要傳遞GetSigmaDateInfo計算索引方法SigmaData的價值?如果這是你正在嘗試做的事情,那麼沿着這些線應該有效。

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); 

} 

如果我有什麼問題讓我知道在評論中,我會嘗試修復從那裏的代碼。在相關說明中,我強烈建議您考慮使用強類型視圖,而不是在ViewBag或ViewData中發送信息。

0

Index()一個可選的參數

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 
} 

然後改變GetSigmaDateInfo到/參數拖着返回RedirectToAction結果W¯¯

[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() }); 
} 
相關問題