Access ASP.NET 5 View Component via URL

元气小坏坏 提交于 2019-12-06 03:01:07

问题


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'.


回答1:


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.


来源:https://stackoverflow.com/questions/30583068/access-asp-net-5-view-component-via-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!