ServiceStack Razor - equivalent to Html.RenderAction

I have a requirement to use Html.RenderAction, as in ASP.NET MVC.

For example, I have a homepage with news and products.

I would like to do for example

@Html.RenderAction("/api/products/featured") 

To start a new service call and output the template to the html stream.

Is this possible using ServiceStack Razor, and if so, how to do it?

+2
source share
2 answers

The PartialExamples.cshtml test page shows various examples of visualizing a razor inside a page, for example:

Using the new RenderToAction() method, which allows you to execute the Service and display a partial view using the route and QueryString, for example:

 @Html.RenderAction("/products/1") 

This also accepts an optional view name if you want a different view than the default:

 @Html.RenderAction("/products/1", "CustomProductView") 

There is also normal Html.Partial() to indicate which view and model you want to display on the page, for example:

 @Html.Partial("GetProduct", base.ExecuteService<ProductService>(s => s.Any(new GetProduct { Id = 1 }))) 

ExecuteService is just a wrapper around an equivalent ResolveService in a using statement, i.e.

 @{ Response response = null; using (var service = base.ResolveService<ProductService>()) { response = service.Any(new GetProduct { Id = 1 }); } } @Html.Partial("GetProduct", response) 

A new RenderToAction() method in Razor Views was added in v4.0.34 + , which is now available in MyGet .

+3
source

* I can duplicate my answer, or I somehow lost it

Looking at the ServiceStack.Razor.ViewPage class, there is an Html property of type ServiceStack.Html.HtmlHelper. I do not see "RenderAction" as a method (or extension method) for this class, so it does not seem to be available. There is a "Partial" method that accepts a ViewName view and an overload that accepts a ViewName view and object. Based on your previous comment, this does not seem to be a useful solution.

If I'm right about the above, I think you will need your “Featured View Template” to pull the data. Can add soemthing like

  { FeaturedResponse products = new JsonServiceClient("http://localhost").Get<FeaturedResponse>("/api/products/featured"); } 

into your template. This will allow you to use a product variable like Model.

Or use javascript to pull data into a template. However, you will have to use JavaScript to retrieve the data in the HTML elements.

Then you can display the template using @ Html.Partial ('Featured')

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1438888/


All Articles