Mvc: How partial pages work in Razor Pages?

Created on 16 Aug 2017  路  5Comments  路  Source: aspnet/Mvc

Hi,

In MVC I have following code:

[HttpPost]
public async Task<PartialViewResult> Promet([FromBody] PrometParam prometParam)
{
   var oid = User.FindFirst(ClaimTypes.PrimarySid).Value;
   var p = await ctx.OsobaPromet.FromSql("EXECUTE sp_elink_FinPromet {0}, {1}, {2}", oid, prometParam.DatumOd, prometParam.DatumDo).OrderByDescending(x => x.DatumDokumenta).ToArrayAsync();
   if (prometParam.Usluga != "Sve")
   {
      p = p.Where(x => x.NazivKonta == prometParam.Usluga).ToArray();
   }

   return PartialView("OsobaPromet", p);
}

How does this translate to Razor Pages? I failed to find any information about partial views in razor pages.

Thanks,
Mario

feature-Razor-Pages

Most helpful comment

This is super critical when ajax is involved and nowadays most if not all sites have lots of ajax.
It will be very useful to be able to do something like(we are talking about razor pages here):

public async Task<IActionResult> OnPostCreateAsync(Something something)
{
    //some logic
    return await PartialAsync(partialViewName, model, viewData);
}

and of course to include the same partial view from the cshtml page : @await Html.PartialAsync(...)

All 5 comments

We certainly have support for rendering a partial page from within another Razor Page from within the markup, i.e. calling @Html.Partial(...).

But I'm not sure we have anything (yet) for returning a partial view from a page action.

@rynowak / @DamianEdwards - thoughts on this?

Is the intent to render a page without the layout? There isn't a feature for this out of the box, but you could always use a flag to influence this behavior:

```C#
public class MyModel : PageModel
{
public bool RenderLayout { get; set; } = true;

public async Task OnPostPromet([FromBody] PrometParam prometParam)
{
RenderLayout = false;
...
}
}

// MyPage.cshtml
@page
@model MyModel
@{
if (!Model.RenderLayout) { Layout = null; }
...
```

We recommend doing ^ that ^.

Hi,

thanks for instructions, seems reasonable solution.

Regards,
Mario

This is super critical when ajax is involved and nowadays most if not all sites have lots of ajax.
It will be very useful to be able to do something like(we are talking about razor pages here):

public async Task<IActionResult> OnPostCreateAsync(Something something)
{
    //some logic
    return await PartialAsync(partialViewName, model, viewData);
}

and of course to include the same partial view from the cshtml page : @await Html.PartialAsync(...)

Was this page helpful?
0 / 5 - 0 ratings