3

ビュー モデル:

namespace AESSmart.ViewModels
{
    public class HomeIndexViewModel
    {
        public power_weatherstationhistory WeatherStationHistory {get;set;}
        public DateTime startingDate {get;set;}
        public DateTime endingDate {get;set;}
        public DateTime utcStartingDate {get;set;}
        public DateTime utcEndingDate {get;set;}
        public double LifeTimeGeneration {get;set;}
        public double CO2Offset {get;set;}
        public double GallonsOfGasolineOffset {get;set;}
        public double BarrelsOfOilOffset {get;set;}
        public string Message {get;set;}
    }
}

コントローラ:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AESSmart.Models;
using AESSmart.ViewModels;

namespace AESSmart.Controllers
{
    public class HomeController : Controller
    {
        private readonly AESSmartEntities db = new AESSmartEntities();

        public ActionResult Index()
        {
            HomeIndexViewModel IndexViewModel = new HomeIndexViewModel();

        if (Convert.ToString(IndexViewModel.startingDate) == 
                                      "1/1/0001 12:00:00 AM" ||
            Convert.ToString(IndexViewModel.endingDate) == 
                                      "1/1/0001 12:00:00 AM") 
            {
                IndexViewModel.startingDate = 
                     new DateTime(DateTime.Now.Year, 
                                  DateTime.Now.Month, 
                                  DateTime.Now.Day, 
                                  0, 
                                  0, 
                                  0);
                IndexViewModel.endingDate = 
                     new DateTime(DateTime.Now.Year, 
                                  DateTime.Now.Month, 
                                  DateTime.Now.Day, 
                                  23,
                                  59, 
                                  59);
            }

            // There is a bunch of code here to gather all of the
            // data need and modify the values of IndexViewModel 

            return View(IndexViewModel);
        }
    }
}

私のインデックスページは次のものを使用しています:@model AESSmart.ViewModels.HomeIndexViewModel次に@Model.Something、ビュー内の各ピースをレンダリングするために使用します。

ビューで使用される、表示する必要があるデータがたくさんあります。ユーザーが変更できるデータは、startingDateとのみEndingDateです。それらのいずれかを変更したら、Index ActionResultそれらHomeControllerの新しい日付を使用して正しい情報を取得する必要があります。現在、デフォルトで同じ日付に戻っています(その日付は今日の日付です)。ここで何が間違っていますか?

また、Index ActionResultは気象情報を収集します。収集した情報を実際にデータベースに保存したい。に含まれる情報を保存するにはどうすればよいWeatherStationHistoryですか?

ユーザーに表示されるサンプルは次のとおりです。

ここに画像の説明を入力

4

2 に答える 2

2

ほとんどのコードをビュー モデルに移動しました。WeatherStationHistory変更を保存するには、データベース コンテキストに追加して変更を保存しました。これで、すべてが希望どおりに機能します。私のコードが今見える方法は次のとおりです。

モデルを見る:

namespace AESSmart.ViewModels
{
  public class HomeIndexViewModel
  {
    public power_weatherstationhistory WeatherStationHistory {get;set;}
    public DateTime startingDate {get;set;}
    public DateTime endingDate {get;set;}
    public DateTime utcStartingDate {get;set;}
    public DateTime utcEndingDate {get;set;}
    public double LifeTimeGeneration {get;set;}
    public double CO2Offset {get;set;}
    public double GallonsOfGasolineOffset {get;set;}
    public double BarrelsOfOilOffset {get;set;}
    public string Message {get;set;}

    public void setUTCDatesTimes()
    {
      //Contains code to convert dates to the UTC equivalent 
    }

    public void setOffsetsAndPowerGenerated()
    {
      /*
       * CONTAINS A BUNCH OF CODE TO GATHER THE GENERATED POWER READINGS
       * FOR THE SPECIFIED DATETIME AND STORES RESULT IN LifeTimeGeneration.  
       * ALSO, PERFORMS CALCULATIONS TO GET AND STORE VALUES FOR CO2Offset, 
       * GallonsOfGasolineOffset, AND BarrelsOfOilOffset
       */
    }

    public void saveWeather()
    {
      AESSmartEntities db = new AESSmartEntities();
      db.PowerWeatherStationHistorys.Add(WeatherStationHistory);
      db.SaveChanges();
    }

    public void setWeather()
    {
      AESSmartEntities db = new AESSmartEntities();
      DateTime tempDate = (DateTime.UtcNow).AddMinutes(-5);

      var myQuery = (from s in db.PowerWeatherStationHistorys
                     where s.recordTime >= tempDate
                     orderby s.recordTime descending
                     select s).Take(1);

      if(myQuery.Count() > 0) 
      {
        /*
         * IF A WEATHER RECORD EXISTS IN THE THE DATABASE NO OLDER 
         * THAN 5 MINUTES THEN USE THAT INFORMATION
         */
      }
      else
      {
        /*
         * IF A RECORD DOES NOT EXIST IN THE THE DATABASE NO OLDER 
         * THAN 5 MINUTES THEN GET WEATHER INFORMATION FROM WUNDERGRAOUND API 
         * THEN SAVE IN DATABASE
         */

        saveWeather();
      }
    }
  }
}

コントローラ:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AESSmart.ViewModels;

namespace AESSmart.Controllers
{
  public class HomeController : Controller
  {
    public ActionResult Index()
    {
      HomeIndexViewModel IndexViewModel = new HomeIndexViewModel();

      IndexViewModel.startingDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
      IndexViewModel.endingDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
      IndexViewModel.setUTCDatesTimes();
      IndexViewModel.setWeather();
      IndexViewModel.setOffsetsAndPowerGenerated();
      IndexViewModel.Message = "Welcome to the Amptech Energy Systems Solar PV Monitoring System";

      return View(IndexViewModel);
    }

    [HttpPost]
    public ActionResult Index(HomeIndexViewModel IndexViewModel)
    {
      if (Convert.ToString(IndexViewModel.startingDate) == "1/1/0001 12:00:00 AM" || 
          Convert.ToString(IndexViewModel.endingDate) == "1/1/0001 12:00:00 AM")  
      {
        return RedirectToAction("Index");
      }

      IndexViewModel.setUTCDatesTimes();
      IndexViewModel.setWeather();
      IndexViewModel.setOffsetsAndPowerGenerated();
      IndexViewModel.Message = "Welcome to the Solar PV Monitoring System";

      return View(IndexViewModel);
    }     
  }
}
于 2012-07-25T19:05:37.927 に答える
1

インデックスに [HTTPPost] メソッドを追加してみましたか?

[HttpPost]
public ActionResult Index(HomeIndexViewModel viewModel) 
        { 

    if (Convert.ToString(viewModel.startingDate) == 
                             "1/1/0001 12:00:00 AM" || 
        Convert.ToString(viewModel.endingDate) == 
                             "1/1/0001 12:00:00 AM")  
        { 
            viewModel.startingDate = new DateTime(DateTime.Now.Year, 
                                                  DateTime.Now.Month, 
                                                  DateTime.Now.Day,  
                                                  0,  
                                                  0,  
                                                  0); 
            viewModel.endingDate = new DateTime(DateTime.Now.Year,  
                                                DateTime.Now.Month,  
                                                DateTime.Now.Day,  
                                                23,  
                                                59,  
                                                59); 
        } 

        // There is a bunch of code here to gather all of the 
        // data need and modify the values of IndexViewModel  

        return View(viewModel); 
    } 
于 2012-07-23T14:58:33.880 に答える