Hello,
I have an Asp.Net Core 2.1, MVC, C# Web application in Visual Studio 2017 15.7.5 using a ViewComponent invoked directly from a controller.
When It gets to the return View(items); it does not show on the screen.
It does display the alert in the startTask() function and shows the alert data.
When I use the Developer Tools/Network tab, it shows the Get for /products/ Response Payload as having the data for the display of the view as indicated by this
screen shot. It seems that the information is there, it just doesn't show on the screen.
I did try these returns:
return View("Default", items);
return View("~Views/Shared/Components/ProductList/Default.cshtml", items);
Here is my code:
ProductListViewComponent.cs
namespace ChinavasionAPI.ViewComponents
{
public class ProductListViewComponent : ViewComponent
{
private readonly ChinavasionAPIContext _context;
private readonly IHubContext<ChvHub> _hubContext;
public ProductListViewComponent(IHubContext<ChvHub> hubcontext, ChinavasionAPIContext context)
{
_context = context;
_hubContext = hubcontext;
}
#region snippet1
public async Task<IViewComponentResult> InvokeAsync()
{
var items = await GetItemsAsync();
await this._hubContext.Clients.All.SendAsync("ReceiveMessage", "", "=============================================== Ready to Display Lakeside and Chinavasion Products ===============================================");
return View(items);
}
public async Task<List<ProductViewModel>> GetItemsAsync()
{
var model = new AccessModel();
model.UserAccessModel = _context.UserAccessModels.Single(a => a.ID == 1);
// Most of the code taken out for brevity..........
await this._hubContext.Clients.All.SendAsync("ReceiveMessage", "", "================================================== Merging Lakeside and Chinavasion Products ==================================================");
var result =
from c in multipleProductModel.MProductsApi
join p in multipleProductModel.MProducts on c.Sku equals p.model_code into ps
from p in ps.DefaultIfEmpty()
select new ProductViewModel
{
Id = c.Id,
Sku = c.Sku,
//Categories = c.Categories,
product_id = p?.product_id != null ? p.product_id : 99999,
model_code = p?.model_code != null ? p.model_code : "Not found",
};
ViewData["ChvProductCount"] = multipleProductModel.MProducts.Count.ToString();
ViewData["ChvProductNotFoundCount"] = chvProductNotFoundCount.ToString();
return result.ToList();
}
AccessToken.cshtml:
@model ChinavasionAPI.Models.AccessModel
@using Microsoft.AspNetCore.Http.Extensions
@{
ViewData["Title"] = "Access Token";
}
@section Scripts {
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script><script src="~/lib/signalr/signalr.js"></script><script src="~/js/chat.js"></script>
}<div><h2>Access Data</h2>
@using (Html.BeginRouteForm("Submit", FormMethod.Post))
{<table><tr><td><label for="serverUrl">Server url: </label></td><td><input style="margin-left:6px;width:400px;" name="serverUrl" type="text" id="serverUrl" value="@Model.UserAccessModel.ServerUrl" /></td><td> </td><td> </td><td><input style="margin-left:6px;" type="submit" value="Get Access Token" /><button style="margin-left:6px;" type="button" id="refresh-token-button">Refresh Access Token</button></td>
@* Most code taken out for brevity *@
<div class="container"><div class="row"><div class="col-md-5"></div><div class="col-md-2"><button class="btn btn-default" id="get-products-button" value="Get Products" style="margin-top:5px" ; onclick="startTask()">Get Products</button></div><div class="col-md-5"></div></div><div class="row" style="background-color:#efefef;"><br /><div class="col-md-12"><ul id="messagesList"></ul></div></div></div><script>
function startTask() {$.get("/products/", function (data) { alert(data); });
}</script><script>
document.getElementById("get-products-button").addEventListener("click", event => {
event.preventDefault();
});</script>
Default.cshtml: (This is in \Views\Shared\Components\ProductList)
@model List<ChinavasionAPI.Models.CAPIViewModels.ProductViewModel>
@{
ViewData["Title"] = "products";
}<h2>Products List</h2><div class="container-fluid"><table class="table table-hover table-bordered table-striped"><thead><tr><th colspan="2" scope="colgroup">Lakeside products = @Model.Count.ToString()</th><th></th><th></th><th colspan="2" scope="colgroup">Chinavasion products = @ViewData["ChvProductCount"]</th></tr><tr><th>Product ID</th><th>Sku</th><th></th><th>Product ID</th><th>Model Code</th></tr></thead><tbody>
@foreach (var item in Model)
{<tr><td><span>@item.Id</span></td><td><span>@item.Sku</span></td><td><span></span></td><td><span>@item.product_id</span></td><td><span>@item.model_code</span></td></tr>
}</tbody></table></div>
ProductsController.cs:
namespace ChinavasionAPI.Controllers
{
public class ProductsController : Controller
{
private readonly ChinavasionAPIContext _context;
public ProductsController(ChinavasionAPIContext context)
{
_context = context;
}
public async Task<IActionResult> GetProducts()
{
return ViewComponent("ProductList");
}
Does anyone know why that Default.cshtml will not display?
Thanks,
Tony