查看模型:
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
?
以下是用户看到的示例: