Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access ASP.NET 5 View Component via URL

With the replacement of partial views in ASP.NET 5 with view components, how does one access the view components via URL?

I know you call them like...

@Component.Invoke("SomeList", 1)

...but what if you need to have like ajax paging, where you need a callback url to request the next set to be displayed in a partial view? So, a user can click "Load More" and it loads more from a 'partial view'.

like image 286
QuaffAPint Avatar asked Jun 01 '15 20:06

QuaffAPint


1 Answers

You cannot access a view component from a URL directly. A view component is just a component of a view and that view could be a regular or partial view.

Based on your question, I believe you are trying to show the first page by default when the view (having the view component) is rendered? I have tried to put some scenarios here.

Example scenario:
Show a snippet on layout page which shows list of available job positions.

Usage cases:

  • Render the html related to a job list at the server side :

    1. Layout page would have something like @Html.Partial("JobsListPartial").
    2. This "JobsListPartial" would have something like await @Component.InvokeAsync("JobsListViewComponent", pageNumber). This partial view also sends ajax script to the client for users to navigate through the pages.
    3. At the client when user tries to navigate to a different page, the ajax script makes a call to a JobsController having an api like IActionResult GetJobs(int pageNumber) and this action returns a PartialViewResult by doing something like return PartialView("JobsListPartial", pageNumber).
  • Render all pages at the client side only :

    1. Create a partial view (having your ajax scripts) and render to the client.
    2. Create a controller exposing api for navigation through pages of available job positions.
    3. Call this api(returns json) from the ajax script.
    4. Use the json data to dynamically change the UI at the client.
like image 102
Kiran Avatar answered Oct 12 '22 02:10

Kiran