I'm starting to learn ASP.NET WebAPI and I cannot seem to understand the marriage between the two. I created an Index.cshtml view with the following markup:
<div>
<div>
<ul data-role="listview" data-inset="true" data-filter="true" data-filter-placeholder="Search..." id="items">
</ul>
</div>
</div>
@section scripts {
<script>
var apiUrl = '/api/items';
$(document).ready(function () {
// Send an AJAX request
$.getJSON(apiUrl)
.done(function (data) {
// On success, 'data' contains a list of items.
$.each(data, function (key, item) {
// Add a list item.
$("ul").append("<li><a href='acura.html'>" + item.ItemCode + "</a></li>").listview("refresh");
});
});
});
</script>
}
This view basically gets sent to the browser, but when the browser receives this view, it makes an ajax call to fetch the data using Web API that I defined. But is it me or am I seeing an unnecessary second call here to get the data. Why don't we stick to the original MVC way of thinking (without WebAPI) where the view sends the markup and the data is already embedded in the markup?
I'm seeing two calls here, one for the view and another for the data, is this really more efficient or am I missing something here?