I'm attempting to make a call to the deprecated Google Finance API (just to learn) parsing the response and post it to my controller. I show the user a view with 0 values but once the response returns, I will display the parsed results.
The results look perfect except that the view is printed to the console instead of being displayed. What am I overlooking here? Any other feedback is also welcome as I'm just trying to learn ASP.NET MVC.
Here is my code:
HomeController:
public ActionResult Index3()
{
var model = from a in realList orderby a.Symbol select a;
return View(model);
}
[HttpPost]
public ActionResult Index3(string JSON)
{
// parse response...
// listOfStocks.Add(...);
var model = from a in listOfStocks orderby a.Symbol select a;
return View(model);
}
static List<Stocks> listOfStocks = new List<Stocks>();
Index3:
@model IEnumerable<WebApplication2.Models.Stocks>
@{
ViewBag.Title = "Stock Texter";
}
<h2>Stock List</h2>
<table class="table table-striped">
<tr>
<th>
@Html.DisplayNameFor(model => model.Symbol)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th>
@Html.DisplayNameFor(model => model.Change)
</th>
<th>
@Html.DisplayNameFor(model => model.ChangePercent)
</th>
<th>
@Html.DisplayNameFor(model => model.PreviousClose)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Symbol)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Change)
</td>
<td>
@Html.DisplayFor(modelItem => item.ChangePercent)
</td>
<td>
@Html.DisplayFor(modelItem => item.PreviousClose)
</td>
</tr>
}
</table>
@section JavaScript
{
<script src="@Url.Content("/Scripts/test.js")"></script>
}
test.js:
(function ($, window, document) {
var data = "";
$.when(
$.getJSON("http://finance.google.com/finance/info?client=ig&q=NASDAQ%3AAAPL,JPM,CAT,MSFT&callback=?", function (response) {
data = response;
console.log("call succcessful");
})
).then(function () {
$.ajax({
type: "POST",
url: "/home/index3",
data: { 'JSON': JSON.stringify(data) },
success: function (response2) {
console.log(response2);
}
});
});
}(window.jQuery, window, document));