gpt4 book ai didi

c# - 我如何使用和保存 View 模型

转载 作者:行者123 更新时间:2023-11-30 16:23:22 26 4
gpt4 key购买 nike

查看模型:

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

Controller :

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 在 View 中呈现每个片段。

View 中使用了一堆数据,只是需要显示。用户可以修改的数据只有 startingDateEndingDate。一旦他们修改了其中任何一个,我希望 HomeControllerIndex ActionResult 使用这些新日期来提取正确的信息。现在它只是默认回到相同的日期(该日期是今天的日期)。我在这里做错了什么?

另外,Index ActionResult 收集天气信息。我希望将收集到的任何信息实际保存到数据库中。如何保存 WeatherStationHistory 中包含的信息?

这是用户看到的示例:

enter image description here

最佳答案

我将大部分代码移到了 View 模型中。为了保存更改,我将 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();
}
}
}
}

Controller :

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

关于c# - 我如何使用和保存 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11614609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com