1

I want to convert this code from this asp.net mvc to asp.net core

[ChildActionOnly]
public ActionResult MyActionThatGeneratesAPartial(string parameter1)
{
    var model = repository.GetThingByParameter(parameter1);
    var partialViewModel = new PartialViewModel(model);
    return PartialView(_partialViewModel); 
}


@{ Html.RenderAction("MyActionThatGeneratesAPartial", "mycontrollerpartial"); }

because I'm more familiar with asp.net mvc, so I don't know what this code looks like in asp.net core

robyiset
  • 27
  • 6
  • https://stackoverflow.com/questions/41353874/equivalent-of-html-renderaction-in-asp-net-core – Chetan Jun 23 '21 at 07:30
  • Does this answer your question? [Equivalent of Html.RenderAction in ASP.NET Core](https://stackoverflow.com/questions/41353874/equivalent-of-html-renderaction-in-asp-net-core) – Chetan Jun 23 '21 at 07:31
  • ```Html.RenderAction``` has been removed in asp.net core. ASP.NET Core used new feature called ViewComponents to achieve same things. Article:- https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-3.1 – Pritom Sarkar Jun 23 '21 at 07:32

2 Answers2

1

There's 2 way to work-around missing .NET CORE RenderAction functionality:

<div id="dynamicContentContainer"></div>
<script>   
    $.get('@Url.Action("MyActionThatGeneratesAPartial", "mycontrollerpartial")', {id : 1}, function(data){
            $("#myActionContentContainer").html(data);
        });
</script>
tartarismo
  • 194
  • 1
  • 10
  • There's also this post that explain how incapsulate in a ViewComponent: https://www.stevefenton.co.uk/2019/08/html-renderaction-equivalent-in-net-core-mvc/ – tartarismo Jun 23 '21 at 07:41
1

i share a sample code blow this. public class aboutusViewComponent : ViewComponent { private readonly IService _services;

    public aboutusViewComponent(IService<AGWPservices> services)
    {
         _services=services;

    }

    public async Task< IViewComponentResult> InvokeAsync()
    {
        var services = _services.WhereByInclude(f => f.IsActive == true && f.IsDeleted == false && f.UseOnAbout == true,i=>i.Children).ToList()[0];

        return View("_aboutus",services);
    }

  
}

and use on html @await Component.InvokeAsync("services")

it useful sample . and u can run with async