【问题标题】:Convert RenderAction partial view ASP.Net MVC to ASP.Net Core将 RenderAction 部分视图 ASP.Net MVC 转换为 ASP.Net Core
【发布时间】:2021-06-23 07:22:25
【问题描述】:

我想将此代码从这个 asp.net mvc 转换为 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"); }

因为我对asp.net mvc比较熟悉,所以不知道这段代码在asp.net core中是什么样子的

【问题讨论】:

标签: c# asp.net-core


【解决方案1】:

有两种方法可以解决缺少的 .NET CORE RenderAction 功能:

<div id="dynamicContentContainer"></div>
<script>   
    $.get('@Url.Action("MyActionThatGeneratesAPartial", "mycontrollerpartial")', {id : 1}, function(data){
            $("#myActionContentContainer").html(data);
        });
</script>

【讨论】:

【解决方案2】:

我分享了一个示例代码来解决这个问题。 公共类 aboutusViewComponent : ViewComponent { 私有只读 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);
    }

  
}

并在 html 上使用 @await Component.InvokeAsync("服务")

它有用的样本。你可以异步运行

【讨论】:

    猜你喜欢
    • 2023-02-02
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多