I'm working on ASP.NET Core application and not sure what is right way to use ajax in it.
For example, I have model:
public class Person
{
public int ID { get; set; }
public string Wiki { get; set; }
public string Name { get; set; }
}
Controller, for show list of items:
public async Task<IActionResult> Index()
{
return View(await _context.Person.ToListAsync());
}
And view:
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
<a asp-action="Details" asp-route-id="@item.ID">@Html.DisplayFor(modelItem => item.Name)</a>
</td>
</tr>
}
</tbody>
</table>
When I press on item I want to get item's details.
I have action Details(int? id)
and view Details.cshtml.
And I created ViewModel:
public class PersonViewModel
{
public string Name { get; set; }
public string Description { get; set; }
public string Birthday { get; set; }
public string Death { get; set; }
public string Image { get; set; }
public string Link { get; set; }
}
But this information I get from Wikidata using ajax.
I can run ajax by pressing link on the item and I get json from Wikidata.
But how I can populate ViewModel? For this I need to call action Details from ajax. But ajax usually used for get information on the current page (to avoid postback). So in this case it doesn't belong here.
Or I can call ajax when loading Detals view and populate page information. But in this case I don't need ViewModel at all.
What is right way to use ajax in my application?