1

[HttpPost]コントローラーを使用してビューから DateTime 値を返しています。コントローラーには、HttpPost メソッドの結果も取得したい別のメソッドがあります。または、HttpPost をビューに戻すこともできます。

私が欲しいのは、LINQ の値を HttpPost メソッドからフォームに表示することです。

ビューを作成するために使用する元の方法は次のとおりです。

  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 インデックスに渡したい、または Index メソッドのアイテムと一緒にビューに渡したい 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# メソッドを試しました。

4

5 に答える 5

1

GetSigmaDateInfo で計算した SigmaData の値を Index メソッドに渡そうとしていませんか? それがあなたがやろうとしていることなら、これらの線に沿った何かがうまくいくはずです.

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 で情報を送信するのではなく、厳密に型指定されたビューを使用することを検討することをお勧めします。

于 2013-10-04T12:22:59.507 に答える
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
}
于 2013-10-04T11:04:27.957 に答える
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 の結果を返すように変更します

[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() } );
}
于 2013-10-05T04:54:48.987 に答える