我遇到了来自OpenWeatherMap的weather API问题。我将数据作为JSON对象获取。我能够在视图中呈现它们,不幸的是我不知道如何将它们存储到我的本地数据库中。我的目标是将它们存储在数据库中,以便我可以在特定的模型中使用它们。
我的模特:
public virtual Humidity Humidity { get; set; }
public Object getWeatherForcast()
{
string url = "http://api.openweathermap.org/data/2.5/weather?q=Aalborg&APPID=MY APPID &units=imerial";
var client = new WebClient();
var content = client.DownloadString(url);
var serializer = new JavaScriptSerializer();
var jsonContent = serializer.Deserialize<Object>(content);
return jsonContent;
}
我的控制器
public JsonResult GetWeather()
{
Plant weath = new Plant();
return Json(weath.getWeatherForcast(), JsonRequestBehavior.AllowGet);
}
我的观点:
<script>
$(document).ready(function () {
$.get("@Url.Action("GetWeather","Weather")", function (response) {
console.log(response);
$("#name").text(response.name);
$("#temp").text(response.main.temp);
$("#humidity").text(response.main.humidity);
});
});
我试图平等地说,从我的一个模型中说出从API到现场的湿度,让我们说水:
public ActionResult Edit([Bind(Include = "Id,Wat,TimeRec")] Water water)
{
if (ModelState.IsValid)
{
water.TimeRec = DateTime.Now;
//I think it should be done here
water.Wat = Json.response.main.humidity;
db.Entry(water).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Id = new SelectList(db.Plants, "Id", "Name", water.Id);
return View(water);
}
如果有人能指出我正确的方向,将会很有帮助
非常感谢